apt命令安装完mysql后,直接输入mysql就可进入mysql,
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
显然这是不安全的,想要设置登录时需要用户名和密码的方式。
网上有很多教程,都是无法解决的,无论改什么都还是直接输入mysql就可进入数据库。
直到后面看到这个教程后才解决这个问题Ubuntu18.04修改密码
- 找到安装时默认生成的默认账户在终端上输入 sudo cat /etc/mysql/debian.cnf 如下显示的账号和密码
root@hecs-x-xlarge-2-linux-20200630100243:~# cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = CgApIoTxZ2gYZ3B0
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = CgApIoTxZ2gYZ3B0
socket = /var/run/mysqld/mysqld.sock
注意,这里默认用户名为debian-sys-maint,默认密码为CgApIoTxZ2gYZ3B0。
2. 使用默认的用户名和密码来进行登录
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql -u debian-sys-maint -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
- 进入mysql数据库修改密码
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> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
# ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
根据错误提示知道是因为新密码不符合mysql默认的密码规范,那么我们就需要更改密码规范或者设置一个满足密码规范的密码
- 查看密码规范
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
- 修改密码长度为6
mysql> set global validate_password_length=6;
# Query OK, 0 rows affected (0.00 sec)
- 设置密码规则为简单
mysql> set global validate_password_policy=0;
# Query OK, 0 rows affected (0.00 sec)
- 重新尝试为root设置新的密码
mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
# Query OK, 1 row affected, 1 warning (0.00 sec)
# Rows matched: 1 Changed: 1 Warnings: 1
mysql> update user set plugin="mysql_native_password";
# Query OK, 1 row affected (0.00 sec)
# Rows matched: 4 Changed: 1 Warnings: 0
mysql> flush privileges;
# Query OK, 0 rows affected (0.00 sec)
mysql> quit;
# Bye
更改成功
- 重启mysql服务
root@hecs-x-xlarge-2-linux-20200630100243:~# service mysql restart;
- 使用mysql登录
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 使用用户名和密码登录
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, 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>
大功告成!!!