简体   繁体   中英

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. I'm getting an error while connecting to remote mysql database from localhost through PHP script.

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.

somebody please guide me how can I resolve this?

If you are running it on Localhost then do it like this

$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.

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.

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 .

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.

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.

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.

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