CentOS7安装MySQL5.7密码查看与修改

优质
小牛编辑
137浏览
2023-12-01

软件版本

CentOS release 6.9 (Final)
mysql Client Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper
mysql Server 5.7.18

检测是否安装MySQLServer

检测下系统有没有自带的mysql:yum list installed | grep mysql

如果已经有的话执行命令 yum -y remove mysql-libs.x86_64 卸载已经安装的 MySQL 。

安装

使用 rpm 包进行mysql服务器的安装,具体下载地址在:https://dev.mysql.com/downloads/mysql/5.7.html

wget http://repo.mysql.com/yum/mysql-5.7-community/el/6/x86_64/mysql-community-server-5.7.18-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.18-1.el6.x86_64.rpm

启动

service mysqld start

查看密码

安装完成之后会自动在 /var/log/mysqld.log 中生成连接的密码, 使用 grep "temporary password" /var/log/mysqld.log 命令查看生成的密码。

初次登录MySQL客户端必须重新设置密码才能进行数据的操作,如下:

[root@localhost src]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18
Copyright (c) 2000, 2017, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

密码设置

不过需要注意的是现在MySQL已经强制要求强密码,已经不能再用弱密码比如“123456”了。如果你设置的密码过于简单,会提示错误:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

set password 命令

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

mysqladmin 命令

mysqladmin -u root password "newpass" # 如果root没有设置密码使用这种方式
mysqladmin -u root password oldpass "newpass" # 如果root设置了密码使用这种方式

使用 update 语句

UPDATE user SET authentication_string = PASSWORD('newpass') WHERE user = 'root';

忘记密码

mysqld_safe --skip-grant-tables&
mysql -u root mysql
mysql> UPDATE user SET authentication_string=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;

密码复杂度的修改

mysql> show variables like "validate_password%";
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      | # 必须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.01 sec)
mysql> SET GLOBAL  validate_password_policy='LOW';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL validate_password_length = 6;
Query OK, 0 rows affected (0.00 sec)
  • LOW政策只测试密码长度。 密码必须至少有8个字符长。
  • MEDIUM政策的条件 密码必须包含至少1数字字符,1 大写和小写字符,和1特别 (nonalphanumeric)字符。
  • STRONG政策的情况 密码子字符串长度为4的或更长时间不能匹配 单词在字典文件中,如果一个人被指定。

再进行密码的修改试试。

创建用户并授权

CREATE USER 'test'@'%' IDENTIFIED BY 'password';
GRANT ALL ON test.* TO 'test'@'%' WITH GRANT OPTION;