php mysqli连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3407281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:37:59  来源:igfitidea点击:

mysqli connect problem

phpmysqlicentos

提问by David Shields

This is weird. I have 2 centos boxes, prod (192.168.0.1) and vm (192.168.0.30). Mysql database sits on prod. App sits on vm. From vm, if I type

这很奇怪。我有 2 个 centos 盒子,prod(192.168.0.1)和 vm(192.168.0.30)。Mysql 数据库位于 prod 上。应用程序位于 vm 上。从虚拟机,如果我输入

mysql -u user -p -h 192.168.0.1 -D mydb

it connects lovely, so port is open and listening on prod but in app, i do

它连接得很好,所以端口是开放的,可以监听产品,但在应用程序中,我愿意

$db=new mysqli('192.168.0.1','user','mypass','mydb');

and I get

我得到

Warning: mysqli::mysqli() [function.mysqli-mysqli]: (HY000/2003): Can't connect to 
MySQL server on '192.168.0.1' (13) in /var/www/vhosts/myapp/httpdocs/dstest.php 
on line 123

Both boxes have exactly same versions php, mysql, mysql.so, mysqli.so

两个盒子都有完全相同的版本 php, mysql, mysql.so, mysqli.so

Any advice?

有什么建议吗?

P.S. This also happens if I try $db=new mysqli('127.0.0.1',... BUT NOT if I try $db=new mysqli('localhost',...

PS 如果我尝试 $db=new mysqli('127.0.0.1',... 但如果我尝试 $db=new mysqli('localhost',...

in case it helps, here is my.cnf on prod:

如果有帮助,这里是 prod 上的 my.cnf:

[mysqld]
set-variable=local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

max_connections = 500
max_user_connections = 150

key_buffer = 2048M

query_cache_limit=4M
query_cache_size=64M
table_cache=2048
tmp_table_size=64M
max_heap_table_size = 256M

# users buffers
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=1M

max_allowed_packet=16M

thread_cache=16
thread_concurrency=8
thread_cache_size=128
innodb_buffer_pool_size = 128M
myisam_sort_buffer_size = 128M

wait_timeout = 240
interactive_timeout = 240
max_allowed_packet=32M

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

回答by a1ex07

Check here. It looks very similar to your issue.

检查这里。它看起来与您的问题非常相似。

Edit: added blog content for posterity:

编辑:为后代添加了博客内容:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on ‘xxx.xxx.xxx.xxx' (13)

One of the things I wish I had found in Google faster when trying to figure out why PHP script refuses to connect to remote MySQL server issuing an error SQLSTATE[HY000][2003]Can't connect to MySQL server on 'xxx.xxxx.xxx.xxx' (13)

I am able to connect from local shell, so first thought was if something wrong with recent Zend Framework upgrade, but after a while I figured out that answer is very simple – SELinux was blocking remote connections from PHP scripts executed by Apache web server. The error code (13) at the end of error message means “permission denied”, so that's the indication to see if you have similar issue or not.

Anyway, login as root and do setsebool -P httpd_can_network_connect=1to make it work.

Of course, think twice because you make web server a bit less secure, so don't do that unless you are sure you need it.

SQLSTATE[HY000] [2003] Can't connect to MySQL server on ‘xxx.xxx.xxx.xxx' (13)

当我试图找出 PHP 脚本拒绝连接到远程 MySQL 服务器并发出错误 SQLSTATE[HY000][2003]Can't connect to MySQL server on 'xxx.xxxx. xxx.xxx' (13)

我可以从本地 shell 连接,所以首先想到的是最近 Zend Framework 升级是否有问题,但过了一段时间我发现答案很简单——SELinux 阻止了来自 Apache Web 服务器执行的 PHP 脚本的远程连接。错误消息末尾的错误代码 (13) 表示“权限被拒绝”,这表明您是否有类似问题。

无论如何,以root身份登录并执行setsebool -P httpd_can_network_connect=1以使其工作。

当然,三思而后行,因为您使 Web 服务器的安全性降低了一点,所以除非您确定需要它,否则不要这样做。

回答by Agath

If that server is local, then try this first :

如果该服务器是本地服务器,请先尝试以下操作:

$db=new mysqli('localhost','user','mypass','mydb'); 

instead of

代替

$db=new mysqli('192.168.0.1','user','mypass','mydb');

$db=new mysqli('192.168.0.1','user','mypass','mydb');

This worked for me.

这对我有用。

回答by Venkat Adapala

restart mysql service via command line by running the below command:

通过运行以下命令,通过命令行重新启动 mysql 服务:

service mysql restart

service mysql restart