MySQL connection details: host, port, socket and examples
Websites and applications on your account connect to MySQL (MariaDB) on the same server.
The details
| Setting | Value |
|---|---|
| Host | localhost |
| Socket | /run/mysqld/mysqld.sock |
| Port | 3306 (only needed for network connections) |
| Database name | the full name with your prefix, for example username_shop |
| Username | the full name with your prefix, for example username_shop |
| Password | the database user's password |
Use localhost, not 127.0.0.1: database users are created for localhost, and localhost connects through the socket, which is also faster.
Examples
WordPress (wp-config.php):
define( 'DB_NAME', 'username_shop' );
define( 'DB_USER', 'username_shop' );
define( 'DB_PASSWORD', 'your-password' );
define( 'DB_HOST', 'localhost' );
PHP (PDO):
$pdo = new PDO('mysql:host=localhost;dbname=username_shop;charset=utf8mb4', 'username_shop', 'your-password');
Laravel (.env):
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=username_shop
DB_USERNAME=username_shop
DB_PASSWORD=your-password
Node.js (mysql2):
const db = await mysql.createConnection({ socketPath: '/run/mysqld/mysqld.sock', user: 'username_shop', password: 'your-password', database: 'username_shop' });
Python (PyMySQL):
db = pymysql.connect(unix_socket='/run/mysqld/mysqld.sock', user='username_shop', password='your-password', database='username_shop')
Connecting from another computer
From your own computer or another server, use the server name shown on the Remote MySQL page and port 3306, after allowing your address there. See Remote MySQL.
Good to know
- New databases use
utf8mb4. Usecharset=utf8mb4in connection strings. - The MySQL Databases page shows the host at the top (Host:
localhost). - For PostgreSQL, the host is
localhostand the port5432: see PostgreSQL databases.
Common questions
Which host do I enter when an installer asks for "Database host"?
localhost. Leave the port empty or at its default.
My app only accepts host and port. What do I enter?
Use the socket where possible. If the app cannot use a socket, ask your hosting provider, because a TCP connection to 127.0.0.1 can be refused for users made for localhost.
Thank you — this helps us improve the article.
Still stuck?
Ask the AI about your exact situation — it answers from this knowledge base — or contact us.
Related articles
- How to create a MySQL database and database user
Create a MySQL (MariaDB) database and a database user in the control panel, add the user to the database, and get the details your website needs.
- Error establishing a database connection or access denied
Fix "Error establishing a database connection" and MySQL "Access denied for user" errors: wrong name prefix, password, privileges, host or remote access.
- How to allow remote MySQL access from your computer or server
Let a desktop tool or an app on another server connect to your MySQL databases on port 3306 by allowing its IP address or range in Remote MySQL.
- How to create a PostgreSQL database and user
Create a PostgreSQL database and user in the control panel, add the user to the database, and connect your app with host localhost and port 5432.