Reputation: 1841
Recently i have purchased a domain "domain.com". The hosting company has the usual CPanel to handle databases.
Using the "MySQL Databases" I:
So far so good.
After that i clicked on PHPMyAdmin and redirected to the PHPMyAdmin webpage. There i see the "database1" that i have previously created. Clicking on that database, i used IMPORT to import a very simple table named "test" [columns (id,name,surname)]. Importing the table created table "test" below the database "database1", which is correct.
After that i have tried to connect to that database using the code below (connect_to_db.php
):
// Create connection
$con=mysqli_connect("domain.com","user1","pass1","database1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error() ."<br>";
}
I have uploaded the connect_to_db.php
into the /home/domain/public_html/
.
When i try to connect to the database1 i get the following error:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'user1'@'xxx.xxx.xxx.xxx' (using password: YES) in /home/karkoona/public_html/connect_to_db.php on line 13
Where xxx.xxx.xxx.xxx
is the public IP of domain.com (if i get it right).
Also on the right of landing page of PHPMyAdmin i see:
user: domain@localhost
Any idea why i cannot access the database with the current user? Is there any mistake in my code?
Thanks.
Upvotes: 0
Views: 3838
Reputation: 1
All you need to do is to add your server IP address to Remote Database Access Hosts in cpanel. It will allow access to your database and the same method is used to allow access when you are using localhost. But all you have to do is to add your computer IP address into remote database.
Upvotes: 0
Reputation: 340
you are not pass $con in connection check
if (mysqli_connect_errno())
replace it with
if (mysqli_connect_errno($con))
Upvotes: 0
Reputation: 19528
You need to grant access to user1 using it's IP to access the remote server:
GRANT ALL PRIVILEGES ON database1.* to 'user1'@'xxx.xxx.xxx.xxx' IDENTIFIED BY 'pass1';
NOTE: replace the relevant info to the current info such as xxx.xxx.xxx.xxx
to the current server IP that will access the remote MySQL.
Since you're using cPanel you can do this to allow the IP:
Upvotes: 1
Reputation: 32710
It may be beacuse :
1) User name or password you are using is wrong
2) User may not have privillage to access the database
Upvotes: 0