This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unable to connect to the database mysql?
I was trying PHP web crawler for website http://astellar.com
I did everything step by step and also create user and database for MySQL. Then connect to database with user as well but it show me an error.
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'switsolu_kll'#'localhost' (using password: YES) in /home/switsolu/public_html/crawler/_db.php on line 43
Cannot connect to database server (Reason: Access denied for user 'switsolu_kll'#'localhost' (using password: YES))
where I am wrong in there?
This error almost always appears when the MySQL password is wrong. Check, double check and then triple check that it is correct.
Often when you created user you did not declare the proper hostname or the request is coming from a host other than 'localhost'. You can create the user for wildcard for all hosts, but sometimes the order of rules messes it up.
delete user and recreate
GRANT ALL PRIVILEGES ON switsolu_aaa.* TO 'switsolu_kll'#'localhost' IDENTIFIED BY 'abc123abc';
don't forget to flush privileges
FLUSH PRIVILEGES;
And depending on your app, you may need to restart it or the server to reset socket connection and retry but shouldn't have to, just retry.
If the localhost doesn't work, then delete user and recreate with 'switsolu_kll'#'%' instead. Don't forget flush privileges; after creating user.
Check User Permission. If you running local, and still have root try:
<?php
$localhost = "localhost";
$mysqlusername = "root";
$mysqlpassword = "";
?>
Related
This question already has answers here:
phpMyAdmin on MySQL 8.0 [duplicate]
(18 answers)
Closed 4 years ago.
I'm using a mac and recently installed phpmyadmin and mysql. While trying to login using root on http://localhost/~Shreyas/phpmyadmin/ I get the following errors.
Cannot log in to the MySQL server
mysqli_real_connect(): The server requested authentication method
unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication
method unknown to the client
In the config.inc.php file I tried changing
$cfg['Servers'][$i]['host'] = 'localhost';
to,
$cfg['Servers'][$i]['host'] = '127.0.0.1';
I then got the following error.
mysqli_real_connect(): (HY000/2002): Connection refused
I even added this line:
$cfg['Servers'][$i]['AllowNoPassword'] = true;
I tried the following links but none of the answers mentioned seemed to work. I also changed the port to 3307 and 3306. None of them worked.
PHP Warning: mysqli_connect(): (HY000/2002): Connection refused
`
phpMyAdmin on MySQL 8.0
This is my first attempt at php and mysql. Any help is appreciated.
EDIT: I uninstalled mysql and re-installed it. Now it works.
Since one of 5.7.x branch small versions there was introduced big change in how mysql works on that matter. You can't anymore log in with root account to phpmyadmin. Workaround is to create new user with all privileges. For example for myself i've created a user named "altroot", assigned to it all permissions and set up a password. I'm able to log in to phpmyadmin with such user. To do this:
mysql -u root -p
and then set up user and password (in this command word password should be replaced with a password with which you want to log in):
CREATE USER 'altroot'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'altroot'#'localhost';
FLUSH PRIVILEGES;
Later probably i will update this post with more details.
This question already has answers here:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES)
(25 answers)
Closed 6 years ago.
Been going through tons of answers but none have exactly matched my issue.
I have a MySQL database running and am attempting to connect in PHP with mysqli. The connection code is as follows:
<?php
if(!isset($_COOKIE["uid"])) {
$servername = "localhost";
$username = "study";
$password = "somepassword";
$dbname = "user_study";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("aww");
}
}
However every time I get the error
mysqli::mysqli(): (28000/1045): Access denied for user
'study'#'localhost' (using password: YES)
I know that the user 'study'#'localhost' has permissions to access this database, because I am able to access it through the MySQL command line just fine. I have tried other accounts such as roots with the same result.
Is there anything else I should be checking?
New information (2/23)
It seems that even when I shut the database down I'm getting an access denied result, meaning it seems to be trying to connect to some other database on the server. How would I ensure it is connecting to the correct one?
I think the error message indicates that PHP was able to contact the MySQL Server using the socket file. (It would have been a different error otherwise.)
There's a couple of reasons you could get this error. If we can successfully connect to MySQL Server with a the mysql command line client like this:
> mysql --no-defaults -h localhost -u study -psomepassword user_study
That's going to rule out a lot of the possible reasons for the failure.
The most reasonable explanation for the error message from PHP is that the password being provided in the connection attempt from PHP does not match the password MySQL is expecting.
Some ideas we should be able to rule out. Privileges on the user_study database have been granted to 'study'#'localhost', e.g.
GRANT SELECT ON user_study.* TO 'user'#'localhost'
On a totally different tack, given the assignment statement:
$password = "somepassword";
And assuming that you wouldn't be supplying the actual password in the question... we're left wondering if the actual password contains characters that are subject to PHP string interpretation, such as backslash character, or a dollar sign.
For debugging, I suggest doing an echo $password; following the assignment, and verify that the string emitted is what is expected.
Another possibility is that there isn't an exact match in the mysql.user table, and the user 'study' is actually matching to a different mysql.user... an entry with an empty user ''#'localhost'.
I'd be taking a look at all of the entries in the mysql.user table where user='study' and user=''.
I also want to rule out the possibility that the mysql command line client using a .mylogin.cnf file.
I'm also tempted to suggest that changes were applied to the mysql.user table and a FLUSH PRIVILEGES statement wasn't executed... but that doesn't jive with the behavior (successful connection) observed in the mysql command line client.
We're assuming obviously that the MySQL Server is running local, on the same machine that PHP is executing on. And we're expecting to connect via the local socket file.
As a test, I'd suggest connecting via TCP. Specifying host as 127.0.0.1. That would require a different entry in the mysql.user table. We'd test connection from the mysql command line client:
> mysql --no-defaults -h 127.0.0.1 -u study -psomepassword user_study
But this gets into a whole host of other configuration issues with the MySQL Server, networking enabled, bind address, DNS name resolution, listening port, iptables, firewall, et al.
--
If the problem was an unsupported authentication protocol, I'd expect a different error. If the problem was the inability to connect to the socket file, I'd also expect a different error.
All of the usual causes for this error seem to be ruled out by a successful connection from the command line client, running on the local machine, connecting to using the same credentials.
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.
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.
Quick question here, I'm new to web hosting and I'm just having a muck around to see what's what. I've created the following PHP document and hosted it on my free 000webhost domain:
<?php
$mysql_host = "mysqlXX.000webhost.com";
$mysql_database = "***********_people";
$mysql_user = "*************_admin";
$mysql_password = "******************";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I have set up (hopefully correctly) a small table in my database with 3 fields:
ID - No Null, auto-increment, integer, primary
First Name - VarChar, Length 20
Last Name - VarChar, Length 20
And entered data for each one.
When I open the file from my control panel, I get the following error:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045):
Access denied for user 'a4935911_people'#'10.1.1.45' (using password:
YES) in /home/a4935911/public_html/MySQL+PHP.php on line 9
AND
Failed to connect to MySQL: Access denied for user
'a4935911_people'#'10.1.1.45' (using password: YES)
Any ideas, anyone?
Wrong argument sequence here:
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
Look at the message: Access denied for user 'a4935911_people'#'10.1.1.45' (using password: YES) where a4935911_people is not a user, it is your database's name as set above $mysql_database = "***********_people";.
Use: Host, user, password, dbname as described in the Documentation.
Solution:
$con=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
argument sequence is following order.
$host ="localhost" // change to your host
$username ="root" // change to your mysql user name by default root
$pass =" " //add password if any
mysql_connect($host,$username,$pass) //don't change the sequence
mysql_select("test") // create the test database in mysql
Two possible reasons:
Wrong username/password - check it carefully.
You can't connect remotely, connections are allowed only via localhost. Check your hosting website for such information. If it's not allowed, you can do nothing.
In Addition to the possible conclusions that #ElonThan provides...
it's also possible that your username lacks the proper privileges to the database you are connecting to.
You need to set up mysql to allow remote connection for a particular user in the MySQL database server i.e. mysqlXX.000webhost.com here . The default syntax is:
grant <permission> on <database> to <user>#<location> identified by <password>
So here you have to use(as you have specified the parameters)-
grant all on ***********_people.* to '*************_admin'#'10.1.1.45' identified by '******************'
So for your case run the following command-
grant all on database_name.* to 'username'#'your_ipaddress' identified by 'password'
Run this command in MySQL command prompt.
This will allow username to connect from your IP using that password and give all permissions on all tables in the specified database.
To allow any user to connect from any IP address and use all the tables in any database use the following syntax-
grant all on *.* to '%'#'%' identified by 'password'
And finally you have to use the following command-
FLUSH PRIVILEGES;
To reload all privileges.
For 000webhost.com (Free Hosting Service ) you should Test your code online by uploading it to 000webhost.com server.
That will solve your problem for sure.