简体   繁体   中英

mysql_connect not working for a site

so I finished a site locally, and I have been trying to start to transfer it to an online server. And this is probably a really stupid question but I've spent the last hour trying to find the answer and I can't. So this is a line of PHP code being run on the server (with the username and password changed, but the hostname the same):

if(!mysql_connect("www.bluestreakaquatic.com","username","password"))
die(mysql_error());

It produces this error:

Can't connect to MySQL server on 'www.bluestreakaquatic.com' (10061)

Can anyone possibly give me any sort of advice?

Your hosting provider probably doesn't allow external mySQL connections for security reasons.

If mySQL runs on the same server as your site, try replacing the domain name with localhost .

If that doesn't work, you need to ask your hosting provider for the correct database server address.

I had recently experienced the same problem.

If you have full access to your server (root privileges required):

Step 1: edit my.cnf (usually located in /etc)

Find the following line: [mysqld] and make sure line skip-networking is commented (or remove line) and add following line:

bind-address=YOUR-SERVER-IP

For example, if your MySQL server IP is 66.166.170.28 then entire block should be look like as follows:

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/English
bind-address    = 66.166.170.28
# skip-networking
.......

Where

  • bind-address : IP address to bind to.
  • skip-networking : Don't listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should be removed from my.cnf or put it in comment state.

Step 2: Grant access to all hosts

Start the MySQL monitor with this command: mysql or /usr/local/mysql/bin/mysql . Your shell prompt should now look like this: mysql> . Run this command:

GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY "PASSWORD";

Where:

  • USERNAME is the username that you use when connecting using your php script.
  • PASSWORD is the password you use when connecting.

You now must flush MySQL's privileges. Run this command:

FLUSH PRIVILEGES;

Run this command to exit MySQL:

exit;

Step 3: restart mysql deamon

/etc/init.d/mysqld restart

or

/etc/init.d/mysql restart

depending on what linux distro is your server currently running.

If you are on a shared hosting environment:

It is unlikely that any hosting provider will allow remote connections to mysql server because of security risks. But you may be lucky if your hosting server uses DirectAdmin control panel or cPanel . If so, follow these steps:

For DirectAdmin:

Goto MySQL Management . Select your database and click on it. You will find a section named Access Hosts . Add a new host: ( % )

干得好

For cPanel:

Goto Remote MySQL under Databases section and add % instead of myhost.com :

干得好

Also see the reference: http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html

如果无法从Web服务器连接到远程mysql服务器,但可以从本地主机连接到远程mysql服务器,则该服务器可能没有授予来自该主机的用户名的权限。

GRANT ALL PRIVILEGES ON database.* TO username@serverhostname IDENTIFIED BY 'password'

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