MySQL Connection String Format
Build a MySQL connection string with the mysql:// URI scheme. Covers host, port, database name, authentication, and common connection parameters.
Detailed Explanation
MySQL URI Format
MySQL connection strings use the mysql:// URI scheme. The format closely mirrors PostgreSQL but with MySQL-specific defaults and parameters:
mysql://[user[:password]@][host][:port][/database][?param=value&...]
Default Values
| Parameter | Default |
|---|---|
| Host | localhost |
| Port | 3306 |
| Username | root |
| Database | (none) |
| SSL | off |
Example Strings
Local development:
mysql://root:password@localhost:3306/myapp
Production with SSL:
mysql://appuser:s3cur3p%40ss@db.example.com:3306/production?ssl=true
Character Set and Collation
MySQL connections often need character set configuration. Common parameters include:
?charset=utf8mb4&collation=utf8mb4_unicode_ci
The utf8mb4 character set supports full Unicode including emojis, while the older utf8 in MySQL only supports 3-byte characters.
Connection Timeouts
MySQL supports timeout parameters to prevent hanging connections:
connectTimeout— Milliseconds to wait for initial connectionwaitTimeout— Seconds a connection can idle before being closed by the server
Key-Value Alternative
Some MySQL drivers prefer key-value configuration:
Host=localhost
Port=3306
Database=myapp
User=root
Password=password
SslMode=Required
This format is common in .NET applications using the MySqlConnector or MySQL.Data packages. In Node.js, both the mysql2 and mysql packages accept object-based configuration that maps directly to these key-value pairs.
Use Case
Setting up a MySQL connection for a web application backend, a WordPress database, or a data pipeline that reads from a MySQL replica.