简体   繁体   中英

mysqli connect via IP address but not using host name

I do test run with mysqli on remote host, Working fine with IP address but its not working with fully qualified domain name. DNS is correctly mapped,I can browse the page using browser and ping also success

$mysqli = new mysqli("www.testdomain.com", "uname", "passwd", "database");

Any suggestions ? mysql is running on Centos 6

First, unless you have a particular reason why you have to add www. before testdomain.com (for example, testdomain.com and www.testdomain.com are pointing to different address), please try again without www. in your host name.

Try nslookup testdomain.com on the machine that hosts your php application(or write a php script and call gethostbyname ), see if php can find the correct IP address from given host name.

If the IP address you got from last step is different from the one you tried, try to troubleshoot DNS resolution problem, or edit /etc/hosts file on the machine that hosts your php application, and point testdomain.com to correct IP address. Some servers may have multiple IP addresses bind to them, but MySQL server only listens to one of them, check /etc/mysql/my.cnf on the machine hosts your MySQL server and look for bind 1.2.3.4 (or other host name/IP address). In case you don't already know, MySQL server only accepts connections from localhost by default.

If your MySQL server if configured to listen to a particular host name/IP address, try bind to 0.0.0.0 so your MySQL server allows connections from all sources. Also there could be a firewall blocking connections from other sources. Here's how to check firewall configuration on CentOS.

If your php script can resolve testdomain.com , try using mysql command-line client to connect to your database from the machine that hosts your php applicaiton. mysql -h testdomain.com -u uname -p . The command-line client may give you more information on why the connection failed.

I'm not sure if you're using hosted VPS services, such as Amazon EC2, or you have your own server machine, but Amazon EC2 does provide out-of-box firewall, it's called "Security Group". Login to your EC2 console, and click "Security Group" on left-hand-side panel, then you can edit your firewall settings on right-hand-side panel.

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