MySQL error 2007 (CR_VERSION_ERROR) occurs when the client and server use incompatible protocol versions or when connecting to the wrong port. This commonly happens with Docker port mapping errors or version mismatches between old clients and new servers. Fixing it requires checking port configuration and ensuring client/server version compatibility.
MySQL error 2007 (CR_VERSION_ERROR) with the message "Protocol mismatch; server version = X, client version = Y" indicates that the client and server are unable to communicate using a compatible protocol version. This error has two main causes: version incompatibility between the MySQL client and server, or attempting to connect to the wrong port for the protocol being used. MySQL supports multiple protocols on different ports—the classic MySQL protocol runs on port 3306, while the X Protocol (used by MySQL Shell and Connectors) runs on port 33060 by default. The error is critical because it prevents any database connection from being established. The client cannot proceed with authentication or queries until the protocol versions match.
First, confirm you're using the correct port for your protocol. MySQL has two main endpoints:
- Classic MySQL protocol: port 3306 (mysql://, standard connections)
- X Protocol: port 33060 (mysqlx://, used by MySQL Shell and some connectors)
Check your connection string:
# If using classic protocol, port should be 3306
mysql -h localhost -P 3306 -u user -p
# If using X Protocol, port should be 33060
mysql -h localhost -P 33060 -u userIf you're connecting to the wrong port, update your connection string or configuration file.
If running MySQL in Docker, verify the port mapping is correct. The format is -p host_port:container_port.
Wrong (common mistake):
docker run -d -p 3306:33060 mysql:latest
# This maps host port 3306 to container's X Protocol port 33060 - WRONG!Correct:
docker run -d -p 3306:3306 mysql:latest
# This maps host port 3306 to container's classic port 3306 - RIGHT!Or in docker-compose.yml:
services:
mysql:
image: mysql:latest
ports:
- "3306:3306" # Correct mapping
environment:
MYSQL_ROOT_PASSWORD: rootConnect to the server (if possible) and verify both versions:
# Check server version
mysql -h localhost -u root -p -e "SELECT VERSION();"
# Check client version
mysql --versionLook for major version differences. For example:
- Client: mysql Ver 8.0.35 (connecting to MySQL 5.7 server)
- Server: 5.7.42
If the major versions differ significantly, upgrade your client:
For MySQL 5.7:
sudo apt-get install mysql-client-5.7For MySQL 8.0+:
sudo apt-get install mysql-client-8.0Or update via Homebrew (macOS):
brew install mysql-client
brew link --force mysql-clientIf you're using an application or connector, update it to match or exceed your server version.
Node.js (mysql2 package):
npm install mysql2@latestPython (mysql-connector-python):
pip install --upgrade mysql-connector-pythonJava (MySQL Connector/J):
# Update your pom.xml or build file to latest version
# Current: 8.0.35 or newer for MySQL 8.0+PHP (mysqli or PDO):
# Update PHP and MySQL extensions
sudo apt-get install php-mysql
# or
php -m | grep mysqlBefore troubleshooting further, verify the port is actually open and listening:
# Using telnet (if installed)
telnet localhost 3306
# Using nc (netcat)
nc -zv localhost 3306
# Expected output: Connection successfulIf the connection fails, the MySQL server may not be running or the port isn't exposed. Check:
# Check if MySQL is running
sudo systemctl status mysql
# Start MySQL if stopped
sudo systemctl start mysql
# For Docker
docker ps | grep mysqlIf you're connecting through a database proxy, connection pooler (PgBouncer, ProxySQL), or firewall, verify it's not corrupting the protocol:
# Check if a proxy is running locally
netstat -tuln | grep 3306
# If multiple services on 3306, verify which is MySQL vs proxy
ss -tlnp | grep 3306For ProxySQL or similar, ensure it's configured for the correct MySQL protocol and version:
# In ProxySQL config
[mysql_servers]
hostgroup_id=0
hostname=192.168.1.100
port=3306
weight=1000
status=ONLINE
max_connections=200Once you've identified the correct port and version, update your application:
Connection string format:
mysql://user:password@hostname:3306/database
# NOT
mysql://user:password@hostname:33060/database # X Protocol portCommon configuration files:
.env
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=mypassword
DB_NAME=mydbDocker environment variables:
docker run -e DB_PORT=3306 myapp:latestX Protocol vs Classic Protocol:
MySQL 8.0+ introduced the X Protocol alongside the traditional MySQL protocol. They run on different ports and require different client libraries:
- Classic MySQL Protocol (port 3306): Used by mysql CLI, mysql2/promise, PDO, JDBC
- X Protocol (port 33060): Used by MySQL Shell, MySQL Connectors, and some ORMs like Prisma (optional)
Attempting to use a classic client on the X Protocol port will always fail with error 2007.
Version Compatibility:
MySQL maintains backward compatibility for 1-2 minor versions, but major version jumps (e.g., 5.7 → 8.0 or 8.0 → 9.0) may require client updates. The protocol changes between major versions are usually transparent to applications, but the underlying libraries may not support deprecated features.
Docker Networking:
In Docker Compose, services communicate via hostname (service name), not localhost. Correct configuration:
app:
image: myapp:latest
links:
- mysql:db
mysql:
image: mysql:latest
ports:
- "3306:3306"Connection string from app: mysql://root:password@db:3306/mydb
SSL/TLS Connections:
If using SSL, ensure both client and server support the same TLS version. MySQL 8.0.28+ requires TLS 1.2+. A TLS version mismatch can sometimes manifest as a protocol error.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL