75% OFF ALL NEW PLANS + 100-DAY MONEY-BACK GUARANTEE
75% OFF ALL NEW PLANS

Accessing a MySQL database through a PHP script

To access a MySQL database from your PHP scripts, you can use localhost or 127.0.0.1 as database server name in your connection string. The database port will depend on the MySQL version of your database. For MySQL 5, you can use 3306 (the default port), while for MySQL 8, you will need to use port 3308. If your script supports UNIX sockets, you can use /tmp/mysql8.sock as socket for MySQL 8.

Here is an example connection string for MySQL 5:

mysqli_connect('localhost', 'mysql_user', 'mysql_password','mysql_database');

Since the default port is used, there is no need to define the port for MySQL 5 connections.

Some applications may not work correctly with ports, so you may encounter issues when connecting to your MySQL 8 database. In such events, you can refer to the following example connection strings for MySQL 8 which use different settings:

mysqli_connect('localhost:3308', 'mysql_user', 'mysql_password','mysql_database');

mysqli_connect('localhost', 'mysql_user', 'mysql_password','mysql_database','3308');

mysqli_connect('localhost', 'mysql_user', 'mysql_password','mysql_database', null, '/tmp/mysql8.sock');

mysqli_connect('127.0.0.1', 'mysql_user', 'mysql_password','mysql_database','3308');

You can find the correct database server settings for MySQL 8 for some of the most popular software applications in our MySQL 8 database server settings article.