0
点赞
收藏
分享

微信扫一扫

Ubuntu20安装 mysql

互联网码农 2022-03-16 阅读 119

删除 Mysql

首先在系统终端中查看MySQL的依赖项,运行命令:
dpkg --list|grep mysql 查看mysql 相关的资源包
删除命令如下:

  • sudo apt-get remove mysql-common
  • sudo apt-get autoremove --purge mysql-apt-config

安装 mysql

  • sudo apt-get update
  • sudo apt-get install mysql-server
    检查mysql服务状态
  • systemctl status mysql.service

ubuntu20 mysql 修改初始密码

参考 https://www.digitalocean.com/community/tutorials/how-to-reset-your-mysql-or-mariadb-root-password-on-ubuntu-20-04

停止服务器
  • sudo systemctl stop mysql
Configuring MySQL to Start Without Grant Tables

sudo systemctl edit mysql

[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking

Press CTRL-x to exit the file, then Y to save the changes that you made, then ENTER to confirm the file name

  • sudo systemctl daemon-reload
  • sudo systemctl start mysql
  • sudo mysql -u root
Changing the Root Password
mysql > FLUSH PRIVILEGES;
mysql >  ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new_password';
设置密码会遇到 : Your password does not satisfy the current policy requirements , 解决方案

原因是密码不符合规定,如下为规则:

  • LOW Length >= 8 characters.
  • MEDIUM Length >= 8, numeric, mixed case, and special characters.
  • STRONG Length >= 8, numeric, mixed case, special characters and dictionary file.
    查看当前密码强度:
mysql > SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| 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.02 sec)
# 更改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password  BY 'Password123#@!';
Query OK, 0 rows affected (0.00 sec)
# 设置口令强度
mysql>  SET GLOBAL validate_password.policy = 0;
Query OK, 0 rows affected (0.01 sec)
# 再次查看密码强度
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 8     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 1     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
# 其实前面稍微复杂点的步骤可以省略,主要用于学习
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password  BY '12345678';
恢复正常设置,不然无法建立账户
$ sudo systemctl revert mysql
Removed /etc/systemd/system/mysql.service.d/override.conf.
Removed /etc/systemd/system/mysql.service.d.
$ sudo systemctl daemon-reload
$ sudo systemctl revert mysql

创建用户

如果出现密码强度问题,使用上述方法进行设置
使用刚刚更改过的密码登录 mysql, 创建用户

mysql> create user 'hive'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.01 sec)
举报

相关推荐

0 条评论