Common SQL Errors

MySQL Error 2021: CR_EMBEDDED_CONNECTION - How to Fix and Prevent

Galaxy Team
August 5, 2025

Error 2021 indicates a failed attempt to use MySQL’s embedded server protocol instead of a normal client connection.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 2021 (CR_EMBEDDED_CONNECTION)?

MySQL Error 2021: CR_EMBEDDED_CONNECTION happens when a client library or tool mistakenly tries to start an embedded MySQL server rather than open a standard network connection. Use the non-embedded client library or set the protocol to TCP/UNIX socket to resolve.

Error Highlights

Typical Error Message

Embedded server

Error Type

Connection Error

Language

MySQL

Symbol

CR_EMBEDDED_CONNECTION

Error Code

2021

SQL State

Explanation

Table of Contents

What is MySQL error 2021 (CR_EMBEDDED_CONNECTION)?

Error 2021 appears when a program linked against the embedded version of libmysqlclient calls mysql_real_connect without an initialized embedded server context. The client library returns CR_EMBEDDED_CONNECTION with the message “Embedded server.”

The condition signals that the request cannot be routed through an in-process MySQL server, so the connection fails before any SQL reaches the server.

Fixing it quickly restores connectivity and prevents application downtime.

When does the error occur?

Developers usually meet the error after switching build flags from -lmysqlclient to -lmysqld or when using mysql --protocol=PIPE on platforms that treat PIPE as an embedded call.

GUI tools compiled with embedded support can also trigger it if the embedded engine is disabled at runtime.

The problem can surface on MySQL 5.7, 8.0 and MariaDB forks because the client library checks the protocol field early and aborts if embedded mode is unsupported.

Why is fixing it important?

Connection failures halt application workflows, scheduled jobs and CI pipelines. Misclassified errors can waste debugging time.

Correcting library linkage or connection flags removes the blocker and prevents repeat outages.

What Causes This Error?

A mismatched client library that expects an embedded server but is linked into an application that should connect over TCP is the primary trigger.

Build scripts that add -DWITH_EMBEDDED_SERVER without enabling mysqld initialization also cause it.

Using mysql_options with MYSQL_OPT_USE_EMBEDDED_CONNECTION=1, or running the mysql CLI with --protocol=PIPE or --protocol=MEMORY on systems where those map to the embedded handler, is another common cause.

How to Fix MySQL Error 2021

First verify which libmysqlclient your binary loads. Use ldd or otool to confirm.

Relink against the standard client library if the embedded variant appears.

If your tool must remain embedded-capable, call mysql_library_init and mysql_server_init before mysql_real_connect, or set MYSQL_OPT_USE_EMBEDDED_CONNECTION to 0 with mysql_options.

Common Scenarios and Solutions

Build pipelines that automatically pick libmysqld during static builds frequently break at runtime. Explicitly add -lmysqlclient to override the default.

On Windows, MINGW builds of the mysql CLI with ENABLE_EMBEDDED limit protocols.

Launch mysql --protocol=TCP or remove the option from ~/.my.cnf to connect normally.

Best Practices to Avoid This Error

Pin the correct client library in your build tooling and CI containers. Keep separate Docker images for embedded and network clients.

Set explicit connection protocols in configuration files. Monitor startup logs for “Trying to use embedded connection” and alert early.

Related Errors and Solutions

Error 2020 CR_EMBEDDED_CONNECTION_FAILURE occurs when the embedded server starts but later crashes.

The fixes overlap: review linkage and runtime flags.

Error 2002 CR_CONNECTION_ERROR reports generic network failures and is resolved by checking host, port and socket permissions.

.

Common Causes

Related Errors

FAQs

Does Error 2021 mean my database is down?

No. It usually means your client library tried embedded mode. The external MySQL server may still be running.

Is the error specific to certain MySQL versions?

It affects MySQL 5.1 through 8.0 and MariaDB when the embedded client library is involved.

Can I ignore the error and retry?

Retrying without changing the protocol will fail again. Fix the linkage or connection settings first.

How does Galaxy prevent this error?

Galaxy packages the non-embedded libmysqlclient and forces TCP or UNIX socket protocols, so embedded misconfigurations cannot surface.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo