php Mysql - 使用 IP 地址连接到远程服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7754757/
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
Mysql - connect to remote server using IP address
提问by Frank
I am using PHP and MySQL to make a database connection, the only change is that I am trying to access my remote server from my local computer using my server IP address.
我正在使用 PHP 和 MySQL 建立数据库连接,唯一的变化是我试图使用我的服务器 IP 地址从我的本地计算机访问我的远程服务器。
mysql_connect("x.xx.xx.x","username","password") or die(mysql_error());
and the error I am getting is Unable to connect to the given host
. The user has all privileges and rights.
我得到的错误是Unable to connect to the given host
。用户拥有所有特权和权限。
回答by Jeremy Smyth
Most default installs of MySQL only listen to the local machine.
MySQL 的大多数默认安装仅侦听本地机器。
Look for the bind-address
setting in my.cnf
on the server, and make sure it's listening on the IP address you're targetting. You may also need to ensure skip-networking
isn'tswitched on!
在服务器上查找bind-address
设置my.cnf
,并确保它正在侦听您的目标 IP 地址。您可能还需要确保skip-networking
未打开!
Alternatively (and less securely!) the following will set it up to listen on all addresses - local and remote:
或者(不太安全!)以下将设置它以侦听所有地址 - 本地和远程:
bind-address = 0.0.0.0
回答by Miguel Prz
Try to add a user (USER-NAME) allowed to connect from the IP:
尝试添加允许从 IP 连接的用户 (USER-NAME):
mysql> GRANT ALL PRIVILEGES ON *.* TO USER-NAME@IP IDENTIFIED BY "PASSWORD";
USER-NAME is the username that you would like to create (like 'widor') IP is the public IP address of your remote connection (like '195.x.y.z')
USER-NAME 是您要创建的用户名(如“widor”) IP 是远程连接的公共 IP 地址(如“195.xyz”)
Of course, limit the privileges you want to grant (ALL PRIVILEGES is probably not your choice)
当然,限制你想要授予的权限(ALL PRIVILEGES 可能不是你的选择)
回答by matsko
Before you try using PHP todo this. Open up your terminal or console and type the following:
在您尝试使用 PHP 来执行此操作之前。打开您的终端或控制台并键入以下内容:
$ mysql -u username -h x.xx.xx.x -p
$ mysql -u 用户名 -h x.xx.xx.x -p
then enter your password
然后输入你的密码
Or on windows then type:
或者在 Windows 上输入:
\path\to\mysql.exe -u username -h x.xx.xx.x -p
\path\to\mysql.exe -u 用户名 -h x.xx.xx.x -p
then enter your password
然后输入你的密码
This will give you a more detailed response as to the authentication issue that's coming up. And you can be 100% sure that your remote login from your IP address works. If it works fine then its something in your PHP code that you're missing.
这将为您提供有关即将出现的身份验证问题的更详细的响应。而且您可以 100% 确定从您的 IP 地址进行的远程登录有效。如果它工作正常,那么它在你的 PHP 代码中是你遗漏的。
回答by varela
1) Check which interface MySQL listen for connections, local (localhost, 127.0.0.1) or remote (0.0.0.0 or IP address). It's in my.cnf. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
1)检查MySQL监听连接的接口,本地(localhost,127.0.0.1)或远程(0.0.0.0或IP地址)。它在 my.cnf 中。http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
2) Check does your user have corresponding privileges. User that logs in from localhost can have different priveleges from remote one. For example 'user'@'localhost' and 'user'@'%'
2) 检查您的用户是否有相应的权限。从 localhost 登录的用户可以拥有与远程用户不同的特权。例如 'user'@'localhost' 和 'user'@'%'