Mysql database connection from php in different conditions
In this topic we will discuss about different types of mysql database connection in php.
The general configuration we do to connect a mysql database from PHP is
<?
$dbhost = ‘localhost’;
$dbuser = ‘root’;
$dbpass = ‘password’;
$conn = mysql_connect(‘$dbhost’,’ $dbuser’, ‘$dbpass’) or die (‘Error connecting to mysql’);
?>
So in my discussions below I will be changing the connection statement only in order to connect successfully.Rest will be the same as you know.
Conditions-1:mysql doesnot have password.
Lets say your mysql server does no have any password.So your coinnection statement will be
$conn = mysql_connect(‘$dbhost’,’ $dbuser’, ”) or die (‘Error connecting to mysql’);
Condition-2:-Mysql port changed
Mysql runs on a default port 3306.But for security reasons people change the mysql port.In our case let’s say I have changed the mysql port to 3309.Then my connect statement will be
$conn = mysql_connect(‘$dbhost:3309′,’ $dbuser’, ‘$dbpass’) or die (‘Error connecting to mysql’);
Condition-3:port same but mysql socket file changed.
If in some servers you need to change the default socket file location i.e /var/lib/mysql/mysql.sock
to some other location like /var/lib/mysql-databases/mysqld2/mysql.sock then how would be our connection statement?We get different location for mysql socket file as sometimes we change the mysql default data directory.
$conn = mysql_connect(‘$dbhost:/var/lib/mysql-databases/mysqld2/mysql.sock’,’ $dbuser’, ‘$dbpass’) or die (‘Error connecting to mysql’);
Condition-4:Port and socket changed
Lets say we changed both port and socket for some security reasons.port cahnged to 3309 and socket to /var/lib/mysql-databases/mysqld2/mysql.sock What will be our connection statement?
$conn = mysql_connect(‘$dbhost:/var/lib/mysql-databases/mysqld2/mysql.sock’,’ $dbuser’, ‘$dbpass’) or die (‘Error connecting to mysql’);