简体   繁体   中英

Cannot connect to mysql 5.0.8 using mysql_connect with password

I can connect to my mysql db using php without a password fine. However, when I add a password in phpmyadmin I can no longer connect. I consistently get the error of:

"Could not connect: Access denied for user 'admin'@'localhost' (using password: YES)"

I've tried using both settings of MySQL password 4.0 and 4.1+ with the same results.

mysql_connect ("127.0.0.1", "admin", "password") or  die('Could not connect: ' . mysql_error());;
mysql_select_db ("datatable");

Does anyone have an idea of what may be going on here?

EDIT: I can access it if I turn off the password. There seems to be some misunderstanding

According to the mysql_connect statement you are showing, you are attempting to connect as admin@'127.0.0.1' . This demands the use of TCP/IP to connect to mysql. If you connect as admin@localhost this uses mysql.sock (the socket file) to connect to mysqld.

You should run this query

SELECT user,host,password FROM mysql.user WHERE user='admin';

You should see admin@localhost defined. You probably won't see admin@'127.0.0.1' . You could add it but it is easier to just code for admin@localhost .

Try changing your code as follows:

mysql_connect ("localhost", "admin", "password") or  die('Could not connect: ' . mysql_error());;
mysql_select_db ("datatable");

If you can connect to mysql without a password, then your mysql installation is not secure.

You need to add a password to admin@localhost and remove anonymous user logins as follows:

UPDATE mysql.user SET password=password('whateverpassword')
where user='admin' and host='localhost';
DELETE FROM mysql.user WHERE user='';
FLUSH PRIVILEGES;

Here is another suggestion: Create the admin@127.0.0.1 user like this:

GRANT ALL PRIVILEGES ON *.* TO admin@'127.0.0.1' IDENTIFIED BY 'whateverpassword';

and try your original code

This might help

steps-

  1. go to phpmyadmin;
  2. click on the database you want to change the pw to (onl left);
  3. click on "privileges";
  4. click on the "edit privileges" icon on the right of the root user for localhost.
  5. go to the "change password" block;
  6. tick the "password" radio button;
  7. enter password and re-type that password;
  8. tick: ... keep the old one.
  9. click "go";
  10. now go open a MySql Console from the WAMPSERVER menu;
  11. it will ask you for a pw – just hit the return since you don't have one yet (next time you go to the console you'll need to enter the new pw that you'r entering here). type the following:

    UPDATE mysql.user SET Password=PASSWORD("xxxxxx") WHERE User="root"; (where xxxxxx is the password you entered in the previous step).

  12. Now type: FLUSH PRIVILEGES;

  13. go to your WAMP or MAMP folder, find the APPS folder, and than click on your PHPMYADMIN folder ( eg my folder called phpmyadmin2.11.6 ) and find the config.inc.php

  14. open config.inc.php with wordpad and find the following text:

$cfg['Servers'][$i]['password'] = '';

now add the password that you used in step number 3 like this :

$cfg['Servers'][$i]['password'] = 'yourPasswordHere';

  1. now save this modification, and close config.inc.php

You should be all set now!

Source - http://www.knowledgesutra.com/discuss/tsilti-php-myadmin-access-mysql-database.html

Your username or password are incorrect. Make sure you typed them correctly, without leaving trailing spaces. Usernames and passwords are case sensitive.

If you tamper with MySQL user data via phpMyAdmin you MUST execute the

FLUSH PRIVILEGES

command in order to make the new rules apply.

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