简体   繁体   English

从本地主机连接服务器数据库时出错

[英]Error while connecting a server database from local host

I want to run PHP script in local system (localhost) and the data to be stored in the server database. 我想在本地系统(localhost)中运行PHP脚本,并将数据存储在服务器数据库中。 I'm getting an error while connecting to remote mysql database from localhost through PHP script. 通过PHP脚本从本地主机连接到远程mysql数据库时出现错误。

Warning: mysql_connect(): Access denied for user 'XXXX'@'ip address' (using password: YES) in  E:\xampp\htdocs\New\example\include\config.php on line 13

I tried using 我尝试使用

$con= mysql_connect("example.com:3306","db username","password"); 
mysql_select_db('db', $con);

I tried using mysqli_connect(...) also but I couldn't connet. 我也尝试使用mysqli_connect(...)但无法连接。

somebody please guide me how can I resolve this? 有人请指导我该如何解决?

If you are running it on Localhost then do it like this 如果您正在Localhost上运行它,请按以下步骤进行操作

$connection = mysqli_connect('localhost','username', 'password', 'database');

When you use mysqli you don't need to use the mysql_select_db as this information is passed in when you create the connection. 使用mysqli时,无需使用mysql_select_db因为在创建连接时会传递此信息。

The main fix that I will stress here is using your first credential passed into the connect variable as 'Localhost' if its local on your machine and you are using xampp or mamp etc then use localhost. 我在这里要强调的主要解决方法是,如果您的本地计算机在您的计算机上并且您使用的是xampp或mamp等,则使用您的第一个凭据作为'Localhost'传递给connect变量,然后使用localhost。

this syntax that you have done mysql_select_db('db', '$con') is wrong when using mysql you DO NOT need to pass a $connection variable into mysql as this only applies for the new mysqli . 使用mysql时,您完成的这种语法mysql_select_db('db', '$con')是错误的,您不需要将$connection变量传递到mysql中,因为这仅适用于新的mysqli

Last word of advice as soon as your comfortable (preferably asap) move away from mysql functions and use mysqli, the move is not too different you just have to learn where to pass in the $conn variables. 最后的建议是,只要您习惯了(最好是asap)离开mysql函数并使用mysqli,此步骤并没有太大不同,您只需要了解在$conn变量中传递的位置即可。

Seams like you have not granted privileges to user. 像您这样的接缝尚未授予用户特权。

Try this on your remote database. 在您的远程数据库上尝试。

~$ mysql -u root -p
Enter Password:

mysql> grant all privileges on *.* to user identified by 'pass' with grant option;

Oh, check out Kenziiee Flavius answer first - that might already solve your problem as you are on localhost. 哦,先看看Kenziiee Flavius的答案-就像在本地主机上一样,这可能已经解决了您的问题。

Log in to your Server via commandline: 通过命令行登录到服务器:

mysql -u username -p 

Create a new user for your remote host: 为您的远程主机创建一个新用户:

CREATE USER 'username'@'192.168.0.1' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'192.168.0.1';

Replace 192.168.0.1 with the ip or hostname of your remote host. 将192.168.0.1替换为远程主机的ip或主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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