无法使用php mysqli连接到远程服务器上的MySQL数据库

无法使用php mysqli连接到远程服务器上的MySQL数据库

问题描述:

I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:

...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error)   {
  die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...

I get the following result:

Connection Failed: (1045) Access denied for user 'myUser'@'my.laptop.ip.address' (using password: YES)

Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp. The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'@'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.

If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.

Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'@'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.

Help, please.

我正在尝试使用mysqli将一些数据插入MySQL数据库(让我们调用模式myDatabase),但不能 成功连接。 这是要连接的代码片段: p>

  ... 
 $ config = parse_ini_file('../ includes / config.ini'); 
 $ username = $ config  ['username']; 
 $ password = $ config ['password']; 
 $ dbname = $ config ['dbname']; 
 $ server = $ config ['server']; 
 $ conn =  new mysqli($ server,$ username,$ password,$ dbname); 
if(!$ conn || $ conn-> connect_error){
 die('Connection Failed:('。$ conn-> connect_errno。  ')'。$ conn-> connect_error); 
} 
 ... 
  code>  pre> 
 
 

我得到以下结果: p> \ n

 连接失败:(1045)用户'myUser'@'my.laptop.ip.address'拒绝访问(使用密码:是)
  code>  pre> 
 
  

以下是有关设置的一些细节,如果它们相关:
代码在我的笔记本电脑上运行Windows 7并使用xammpp附带的PHP 5.3.5。 数据库托管在远程 服务器与MySQL5.1.52。 我创建了一个用户,我在myDatabase上授予了所有权限。*。 没有为用户指定主机(例如'myUser'@'%'),因为我仍在开发中,并且不知道将托管实时应用程序代码的IP地址。 p> \ n

如果我ssh到数据库服务器上,我可以使用myUser的凭据连接到mysql并访问myDatabase中的表。 我在同一台服务器上有另一个架构,由不同的用户访问,并且能够使用mysqli连接而没有任何问题。 p>

只是为了确保它不是一个错字 ,我删除了用户,并再次创建它,复制并粘贴我的PHP代码中使用的config.ini文件中的用户名和密码(当然还有刷新的权限)。 我再次这样做,除非这次指定了主机,例如 创建用户'myUser'@'my.laptop.ip.address'通过'myPassword'识别。 我一直得到同样的错误,现在我完全被难倒了。 p>

请帮助。 p> div>

Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.

Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?

On your mysql machine hit:

GRANT ALL PRIVILEGES ON database.* TO 'myUser'@'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;

This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.