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');
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.
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.
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: " .
$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
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
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With