制作测试脚本:
<?php
$a = new PDO('mysql:dbname=mydbname;host=mydbhost:3306', 'myusername', 'mypassword');
var_dump($a);
我收到以下错误:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mydbhost:3306' (107)' in /users/pvarney/test_pdo.php:3 Stack trace:
#0 /users/pvarney/test_pdo.php(3): PDO->__construct('mysql:dbname=my...', 'myusername', 'mypassword')
#1 {main} thrown in /users/pvarney/test_pdo.php on line 3
然后通过CLI:
[pvarney@ci-server ~]$ mysql mydbname -h mydbhost -u myusername -p --port=3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 828511
... snip mysql welcome text ...
mysql>
我已将连接字符串更改为屏蔽连接信息,但我将其复制粘贴到CLI中以确保我没有任何拼写错误。
答案 0 :(得分:4)
变化
new PDO('mysql:dbname=mydbname;host=mydbhost:3306', 'myusername', 'mypassword');
到
new PDO('mysql:dbname=mydbname; host=mydbhost; port=3306;', 'myusername', 'mypassword');
答案 1 :(得分:3)
查看PDO_MYSQL_DSN的PHP文档。该端口应作为单独的参数传递:
mysql:dbname=mydbname;host=mydbhost;port=3306
答案 2 :(得分:2)
看一下这篇文章: Not able to connect to MySQL server using PDO
尝试更改端口设置
答案 3 :(得分:2)
$a = new PDO('mysql:dbname=mydbname;host=mydbhost;port=3306', 'myusername', 'mypassword');
/*note ^^*/
答案 4 :(得分:0)
这对我有用: