Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO cannot connect remote mysql server

server A(192.168.1.3)

mysql server(5.6.12) port 6603,socket /var/run/mysql/mysql.sock

php(5.5.0) php.ini pdo_mysql.default_socket = /var/run/mysql/mysql.sock

server B(192.168.1.4)

mysql server(5.5.11) port 3306,socket /var/run/mysql/mysql.sock

In server A is work when use

$conn = new PDO('mysql:hostname=localhost;dbname=DB_TEST','username','password');

but cannot connect to server B when use

$conn = new PDO('mysql:hostname=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

ERROR:SQLSTATE[28000] [1045] Access denied for user 'username'@'localhost' (using password: YES)

but work on

$conn = mysql_connect('192.168.1.4:3306', 'username', 'password');
like image 876
consatan Avatar asked Jul 13 '13 13:07

consatan


People also ask

How to connect PHPMyAdmin to MySQL?

Create MySQL Database at the LocalhostOpen your browser and go to localhost/PHPMyAdmin or click “Admin” in XAMPP UI. Now click Edit privileges and go to Change Admin password, type your password there and save it. Remember this password as it will be used to connect to your Database.

Does PDO work with MariaDB?

On the other hand, once you've mastered PDO, you can use it with any database you desire, which can be incredibly useful when switching from another database to, say, MariaDB.

How to connect to localhost database with PHP?

php $servername = "localhost"; $database = "database"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " .


3 Answers

$conn = new PDO('mysql:hostname=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

should be

$conn = new PDO('mysql:host=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

hostname is invalid for dsn and so PDO is ignoring host and using default, which is localhost

like image 147
bansi Avatar answered Sep 23 '22 13:09

bansi


ok i had the same problem too. the solutions is the space between

mysql: host --> this work very well!!!

this way you can connect to remote mysql

like image 38
Val Luis Avatar answered Sep 22 '22 13:09

Val Luis


The problem on remote PDO mysql conex are on the db string. The correct statement is:

$conn = new PDO('mysql:host=192.168.1.4:3306;dbname=DB_TEST','username','password');
like image 32
Luis Morales Avatar answered Sep 23 '22 13:09

Luis Morales