MySQL JDBC connection URL format
This is the generic syntax for MySQL Connection URL using the Connector/J driver:
protocol//[hosts][/database][?properties]
1. Protocol
There are four possible protocols for a connection:
- jdbc:mysql: is for ordinary and basic failover connections.
- jdbc:mysql:loadbalance: is for configuring load balancing.
- jdbc:mysql:replication: is for configuring a replication setup.
- mysqlx: is for connections using the X Protocol.
2. Hosts
If it’s a single host, such as localhost, the hosts part is in the format host:port and the URL syntax is as simple as this:
jdbc:mysql://localhost:3306/sakila
- host can be an IPv4 or an IPv6 host name string. When not specified, localhost will be used.
- post can be an integer from 1 to 65535. The default port number for MySQL connection is 3306, and for a connection using the X Protocol is 33060. If not specified, the default port will be used.
You can specify user credentials in the URL:
user:password@host
For example:
jdbc:mysql://myuser:secret_password@localhost:3306/db
When there’re multiple hosts, the hosts will be listed in a comma-separated list:
host1,host2,...,hostN
Then the URL syntax will be like this:
jdbc:mysql://myhost1:port1,myhost2:port2/db
If you want to include user credentials and allow sharing by all hosts, put the list of hosts inside the square blankets:
jdbc:mysql://myuser:secret_password@[myhost1:port1,myhost2:port2]/db
3. Database
This to specify the default database to be opened when connected to the server. If there’s no database specified, the connection is made with no default database.
4. Properties
A succession of global properties applying to all hosts, preceded by ?
and written as key=value
pairs separated by the symbol &
For example:
jdbc:mysql://myuser:secret_password@localhost:3306/db?key1=value1&key2=value2&key3=value3
Or:
jdbc:mysql://myuser:secret_password@[myhost1:port1,myhost2:port2]/db?key1=value1&key2=value2&key3=value3
Need a good GUI tool for databases? TablePlus provides a native client that allows you to access and manage Oracle, MySQL, SQL Server, PostgreSQL, and many other databases simultaneously using an intuitive and powerful graphical interface.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS