MySQL - Connection

While working with MySQL database, we use a client program to communicate with the database server. To do so, we must first establish a connection between them.

To connect a client program with MySQL server, we must ensure all the connection parameters are properly used. These parameters work just like any other login parameters: consisting of a username and a password. Where, a username is the name of the host where the server is running and a password needs to be set according to the user.

Generally, each connection parameter holds a default value, but we can override them either on the command line or in an option file.

This tutorial only uses the mysql client program to demonstrate the connection, but these principles also apply to other clients such as mysqldump, mysqladmin, or mysqlshow.

Set Password to MySQL Root

Usually, during the installation of MySQL server, we will be asked to set an initial password to the root. Other than that, we can also set the initial password using the following command −

mysql -u root password "new_password";

Where, new_password is the password set initially.

Reset Password

We can also change the existing password using the SET PASSWORD statement. However, we can only do so after logging in to the user account using the existing password. Look at the query below −

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password_name');

Every time a connection is needed to be established, this password must be entered.

MySQL Connection Using MySQL Binary

We can establish the MySQL database using the mysql binary at the command prompt.


Here is a simple example to connect to the MySQL server from the command prompt −

[root@host]# mysql -u root -p
Enter password:******

This will give us the 'mysql>' command prompt where we will be able to execute any SQL query. Following is the result of above command −

The following code block shows the result of above code −

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2854760 to server version: 5.0.9

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

In the above example, we have used root as a user but we can use any other user as well. Any user will be able to perform all the SQL operations, which are allowed to that user.

We can disconnect from the MySQL database any time using the exit command at mysql> prompt.

mysql> exit

MySQL Connection Using PHP Script

We can open/establish connection to MySQL database using the PHP mysqli() constructor or, mysqli_connect() function. This function takes six parameters and returns a MySQL link identifier on success or FALSE on failure.


Following is the syntax to open a MySQL connection using the constructor mysqli() −

$mysqli = new mysqli($host, $username, $passwd, $dbName, $port, $socket);


Following are its parameters −

Sr.No. Parameter & Description


Optional − The host name running the database server. If not specified, then the default value will be localhost:3306.



Optional − The username accessing the database. If not specified, then the default will be the name of the user that owns the server process.



Optional − The password of the user accessing the database. If not specified, then the default will be an empty password.



Optional − database name on which query is to be performed.



Optional − the port number to attempt to connect to the MySQL server.



Optional − socket or named pipe that should be used.

Closing the Connection

We can disconnect from the MySQL database anytime using another PHP function close(). Following is the syntax −



Try the following example to connect to a MySQL server. Save the file as mysql_example.php −

      <title>Connecting MySQL Server</title>
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass);
         if($mysqli->connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli->connect_error);
         printf('Connected successfully.<br />');


Access the mysql_example.php deployed on apache web server and verify the output.

Connected successfully.