I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.
I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:
$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");
I've been getting the following error:
Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'#'localhost' (using password: YES)Access denied for user 'cPanelUsername'#'localhost' (using password: NO)
"localhost" is the host server where the MySql server is located (it seems like this works)
"cPanelUsername" is my cpanel username
"dbUsername" is the database user, which I added to the database with all permissions granted
"dbPassword" is the database password for dbUsername
"dbName" is the database name
I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.
It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?
First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.
after that check it again,
first try normal connection code in php,
$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
In cPanel, make sure that:
The database user cPanelUsername_dbName exists, with the password dbPassword
The database you want to use exists.
The user cPanelUsername_dbName is allowed to access the database.
The user cPanelUsername_dbName is allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.
Your MySQL connections may use 127.0.0.1 or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.
check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.
For example:
cpanel username: jack,
database created: student
In your php script, the dbname should be jack_student
This worked for me:
Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.
If you need to change the password for MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhost as the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.
Related
I have a database that I want to transfer to my remote server from localhost. The file being too big, I wrote the code to copy the table. But the problem the connection is not successful. My code looks like this.
$server_conn=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if (!$server_conn) {
die("Server Connection Fail: " . mysqli_connect_error());
}
Where $db_host is the ip address of my server.
When I load the script the error I get is
Server Connection Fail: Access denied for user db_user#'117.202.126.83' (using password: YES)
The credentials are correct, the host is correct, and the weirdest part is 117.202.126.83 is my IP address.
I don't know how is it ignoring the host and taking my connection IP address for host. Even when I give the db_host as the IP address of my remote server.
Also, I am running the script using Easy PHP 14.1 running APACHE 2.4.7,MYSQL 5.6.15 and PHP 5.4.24.
What am I missing?
UPDATE: I am not sure if I was stupid or if the hostgator instructions were.
They ask us to add the cpanel username followed by an underscore before the database name and database username.
In the cpanel, the cpanel username is prefixed by force to all user and db names. So in my case, I had the variables as cpanelUsername_cpanelUsername_databaseName and so on.
Thank you guys for your replies
Go to the cpanel where your phpMyAdmin has the access.
Click on Remote MySQL
Add the access host (Your local IP which is running the script)
Then you can get access to remote mysql server.
You can then test the connection using MySQL Workbench ()
are you sure you use the proper external database hostname/ip?
I get this error message
Access denied for user 'user'#'localhost' (using password: YES)
I tried to access with the root, the admin account and some user account I made for the web visitors with a few privileges. Those users (admin and web user) were created with cPanel.
I'm testing the connection with this simple code to avoid making a mess with functional code.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
I stumbled with this explanation but it doesn't work either
Your cPanel username and password can be used to connect to your databases (as well as your cPanel). If you're connecting to your database using your cPanel username and password, you can reset your cPanel password to ensure you are using the correct username and password.
If you setup a MySQL username and password specifically for accessing a database, you'll want to ensure you are using the correct username in your php scripts. For example, MySQL usernames are always in this format:
cpanel-username_mysql-username
If your cPanel username is userna5 and you created a database username of dbuser1, then the actual database username would be:
userna5_dbuser1
Edit: i already assigned the users to a database and their privileges. I'm working on a remote server
I faced the exact same issue on one of my employees' Windows PC.
The issue was caused duo the fact that his private repositories folders including those who were used by Docker were located in C:\Users{user_name} folder.
At some point in time, Windows prevented Docker's access to these folders.
Solution: relocation the folders outside the Users folder solved the issue.
Are you running the code on your local machine or on the remote server. If it's running on your local machine you'll have to replace 'localhost' with the ip address of the server eg. '123.123.123.123'
If you are using the online server after creating the user and the database make sure you give the user privilege to access the database
If you are accessing MySQL database from the remote server, you will have to use server IP address for MySQL host. Also you will have to allow your remote server IP address in "Remote MySQL" under your cPanel otherwise you will not be able to access your database remotely.
Recently i have purchased a domain "domain.com". The hosting company has the usual CPanel to handle databases.
Using the "MySQL Databases" I:
Created a database with the name "database1"
Created a user with the username "user1" and password "pass1"
Added user "user1" to the database "database1"
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.
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:
Login to your cPanel (if not already logged in).
Scroll down to the Databases section.
Click on the Remote MySQL icon.
Enter the IP address of the computer that will be making the remote connection.
Click the Add Host button.
It may be beacuse :
1) User name or password you are using is wrong
2) User may not have privillage to access the database
you are not pass $con in connection check
if (mysqli_connect_errno())
replace it with
if (mysqli_connect_errno($con))
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.
how to connect cpanel db from Localhost.
i know connect from localhost. But i need to connect from server.
i am trying like this
<?php
mysql_connect("208.91.199.141","username","password") or die(mysql_error());
mysql_select_db("db");
?>
i get an Error
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'glansade'#'49.206.54.187' (using password: YES) in C:\xampp\htdocs\calendar\db_server.php on line 2
Access denied for user 'glansade'#'49.206.54.187' (using password: YES).
How to solve this....? Please Help me. Thanks
Go to RemoteMysql from cpanel
you have to add %.%.
Then you will be able to connect from remote Db.
You will not want to add %.% to Remote Database Access Hosts as it is a security concern. The %.% is a wildcard and using that wildcard in access hosts like that will expose mysql to the internet and the possibility of unauthorized database access is greatly increased.
Based on your error, the IP you are connecting from is 49.206.54.187. You will want to use this IP address in Remote Database Access Hosts. If you do not know the IP address to use, you can check your IP at http://cpanel.net/myip
I also see that you are connecting with your cPanel account username. While this should work, it is the preference of many to setup a separate account for each Database. To accomplish this, follow these instructions.
In cPanel, select MySQL Databases
In the Databases section, under "MySQL Users", create a New MySQL user.
After creating that user, add that user to the Database in the section "Add User To Database"
Make sure to select all privileges.
Thanks!
I'm not an expert, but I think you should try :
mysql_connect("localhost","username","password") or die(mysql_error());
Instead of :
mysql_connect("208.91.199.141","username","password") or die(mysql_error());
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
This is the code I am using to check if I am able to connect to Mysql.
I am having problem connecting using this code. Should I change localhost to the name of the website?
I tried ("www.abc.com","login username", "password") even this is not working.
EDIT:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES)
If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:
> cPanel login: foo
> Database User: bar
> Database: ey
Your database user will be foo_bar and your database will be called foo_ey
Localhost refers to where the database is running. If it's running on the same server as your PHP server localhost should work fine. What kind of problems are you having? Are you sure that your username/password is correct?
You should replace :
localhost by the name of the server on which your MySQL server is
if your MySQL server is on the same server as PHP, then it's localhost
username by the login that allows you to access your database
password by the corresponding password
Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it.
Didn't you hosting company provide you with any information related to MySQL ? Using those informations, what error message does mysql_error() give ?
Do you have a mysql user which you've set up? You'll need to do that. In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. Put that users' username and password in.
You need to pay attention to the error, if any, reported by mysql_error(). This should give you some information you can use to diagnose the connection problem.
See this MySQL Forge wiki page about connection failure:
http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer
Or this page from the MySQL Manual:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html