I recently installed MySQL on the ubuntu system, but there was no prompt to enter the root user password during the installation. After looking for a solution on the Internet for a day, I tried to modify the login password of the root user:
1. Installation
You can refer to the link: https://blog.csdn.net/zou_albert/article/details/114932025
$ sudo apt-get install mysql-server
$ apt install mysql-client
$ apt install libmysqlclient-dev
2. Log in to MySQL
mysql -u root -p
Enter the password, if you can enter, you don’t need to read the following; if you are prompted to log in, we try to enable the safe mode to log in to MySQL, so that you can bypass the password to log in, and then change the password after logging in.
3. Log in to MySQL in safe mode
$ sudo /etc/init.d/mysql stop
-------------------------------------
[sudo] wl 的密码:
[ ok ] Stopping mysql (via systemctl): mysql.service.
$ sudo /usr/bin/mysqld_safe --skip-grant-tables --skip-networking &
Enter the first line to terminate MySQL operation. If it is successful, the following two lines will be prompted; enter the fourth line, if it is successful, you can open another terminal window for the next step; however, an error will generally be reported, such as mysqld_safe Directory'/var /run/mysqld' for UNIX socket file don't exists
Solution: So we tried to enter the following code:
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld
Finally enter again:
sudo /usr/bin/mysqld_safe --skip-grant-tables --skip-networking &
At this point, no error will be prompted. You can open another terminal port and try to log in to MySQL without a password.
mysql -u root
You should be able to enter MySQL at this point, continue to operate
> use mysql;
> update user set authentication_string=PASSWORD("这里输入你要改的密码") where User='root'; #更改密码
> update user set plugin="mysql_native_password"; #如果没这一行可能也会报一个错误,因此需要运行这一行
> flush privileges; #更新所有操作权限
> quit;
4. Use the modified password to log in to MySQL
After the above series of operations, you should be able to log in normally with the password you changed.
> sudo /etc/init.d/mysql stop
> sudo /etc/init.d/mysql start # reset mysql
> mysql -u root -p
The first line terminates the database operation first, the second line restarts the database service, and the third line logs in as the root user.