简体   繁体   中英

cant connect database server using ip address

I am using PHP and MySQL to make a database connection , problem is :

the web application project in my local machine when am trying connect to database remote server every thing is work fine

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 !! 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
any idea !!!

You need to check two things. First, make sure the port 3306 is open for connections. 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. If the user you're trying to connect to has a Host of localhost , you won't be able to connect remotely.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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