安装数据库服务端和客户端
sudo apt install mysql-server mysql-client
安装过程中记得等一下,否则会忘记设置密码,像我就直接Yes而忘记设置密码,不过有相应解决方案。
修改配置,无密码登录
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 在这个配置文件中的[mysqld]这一块中加入 skip-grant-tables 这句话 。
重启服务器 : service mysql restart
登录: mysql -u root -p 回车,输密码再回车进入mysql命令行
按如下命令教程操作,设置root和密码
whch@whch:/usr/share/mysql$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-0ubuntu0.19.04.1 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> update user set authentication_string = password("123"),plugin='mysql_native_password' where user = 'root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye
删除第二步中的那一行
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 在这个配置文件中的[mysqld]删除 skip-grant-tables 这句话 。
重启服务器 : service mysql restart
登录: mysql -u root -p 回车,输密码 123 登录