简体   繁体   English

无法使用IP地址连接数据库服务器

[英]cant connect database server using ip address

I am using PHP and MySQL to make a database connection , problem is : 我正在使用PHP和MySQL建立数据库连接,问题是:

the web application project in my local machine when am trying connect to database remote server every thing is work fine 尝试连接到数据库远程服务器时,本地计算机上的Web应用程序项目一切正常

mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());

when i deploy the web application in the remote server that have the mysql database and try to use that web application am getting no database error connection !! 当我在具有mysql数据库的远程服务器中部署Web应用程序并尝试使用该Web应用程序时,没有数据库错误连接! if i change database connection to : 如果我将数据库连接更改为:

mysql_connect("localhost","username","password") or die(mysql_error());

using localhost (127.0.0.1) instead of using ip address ever thing work fine , i have tried check /etc/my.conf file and add bind-address=192.168.15.15 and restart mysql server still cant connect to database 使用本地主机(127.0.0.1)而不是使用ip地址可以正常工作,我尝试检查/etc/my.conf文件并添加bind-address = 192.168.15.15并重新启动mysql服务器仍无法连接到数据库
any idea !!! 任何想法 !!!

You need to check two things. 您需要检查两件事。 First, make sure the port 3306 is open for connections. 首先,确保端口3306已打开以进行连接。 Second, if you're connecting remotely, you'll need to create a wildcard user (The host will be a percentage sign % ). 其次,如果要远程连接,则需要创建一个通配符用户(主机将是百分比符号% )。 Take a look at the mysql.user table. 看一下mysql.user表。 If the user you're trying to connect to has a Host of localhost , you won't be able to connect remotely. 如果您要连接的用户的Hostlocalhost ,则将无法进行远程连接。

To create a new user: 要创建新用户:

CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';

Give that user permissions, I suggest to only the database in question: 授予该用户权限,我建议仅对有问题的数据库:

GRANT ALL PRIVILEGES ON your_database.* TO 'newuser'@'%';

Finally, reload the privileges: 最后,重新加载特权:

FLUSH PRIVILEGES;

问题是我启用了SElinux,所以我用commad启用了httpd网络:

setsebool -P httpd_can_network_connect=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM