简体   繁体   中英

Cannot set password for MySQL root user

I am trying to get MySQL running on my localhost. This never happened to me before, but upon installation sudo apt-get install mysql-server did not ask me for the initial password.

After searching for answers to "reset" my password and battling with directory permissions, i am currently at this state:

The mysqld_safe solution:

  • After rebooting my computer, mysql -u root -p will ask me for my password, and after i enter it, it will complain with ERROR 1698 (28000): Access denied for user 'root'@'localhost'
  • If i instead try mysql -u root (no password), it will say ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  • If i run the advice that other people have posted about resetting the password via mysqld_safe --skip-grant-tables , then i run across some hiccups along the way:
    • First i run sudo service mysql stop
    • Then sudo mysql_safe --skip-grant-tables will say mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
    • I can fix that if i do sudo mkdir -p /var/run/mysqld and sudo chown mysql /var/run/mysqld
    • After this, running mysqld_safe will run with the message Starting mysqld daemon with databases from /var/lib/mysql and then hang. And Ctrl + C doesn't get me out of that.
    • In a new terminal window i can now actually connect with mysql -u root and get the mysql> prompt.
    • I can set a new password with use mysql; update user set authentication_string=password('0000') where user='root';flush privileges; use mysql; update user set authentication_string=password('0000') where user='root';flush privileges; which will tell me it successfully updated the row with the message Rows matched: 1 Changed: 1 Warnings: 1
    • The warning only states: 'PASSWORD' is deprecated and will be removed in a future release.
    • And after all this, i'm back at square one: The process still hangs, so i have to reboot because killall mysqld_safe says no process found . After rebooting and entering my new password 0000 , i will get access denied.

The system is Linux 4.15.0-48-generic #51-Ubuntu SMP .

The mysqladmin solution

If i run mysqladmin -u root password '0000' (without sudo ), i get:

mysqladmin: connect to server at 'localhost' failed

error: 'Access denied for user 'root'@'localhost''

If i run it with sudo , i get:

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

This last one doesn't really tell me anything in regards of whether the password change happened or not; has it been refused or not (since it's a warning, not an error, i would assume it should go through), but in either case, trying to connect with 0000 still says "access denied".

Any advice on how to fix this is greatly appreciated.

I didn't quite solve the problems with mysql directly; However for myself, the acceptable solution was switching to a different database software, but it's still a MySQL system, and it got me up and running. So if anyone gets stuck on existing solutions not working like i did, maybe this comes in handy to get you on the road and finally writing code again.

What i did was:

  1. Purged mysql off my system
  2. Manually deleted /etc/mysql and /var/lib/mysql (don't know if this step was necessary)
  3. apt clean , apt autoremove , apt update
  4. Installed mariadb
  5. Followed these instructions to rid myself of the timeout bug
  6. Used the mysqld_safe --skip-grant-tables method to create a new user with all privileges, since connecting with root always kept failing

PHP + MariaDB now working again.

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