当前位置:首页 - 博客 - 正文

Centos安装mysql 5.7

本文安装的mysql版本为5.7,需要安装mysql 8.0以上可自行换yum文件

1、下载和安装mysql

  1. yum -y install https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
  2. sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/mysql-community.repo
  3. yum clean all
  4. yum -y install mysql-community-server mysql-community-client

2、配置mysql

  1. vi /etc/my.cnf

添加如下内容:

  1. [mysqld]
  2. log-error=/var/log/mysqld.log
  3. pid-file=/var/run/mysqld/mysqld.pid
  4. character-set-server = utf8
  5. collation-server = utf8_general_ci
  6. init_connect = "SET NAMES 'utf8'"
  7. explicit_defaults_for_timestamp=true
  8. [mysql]
  9. default-character-set = utf8
  10. [client]
  11. default-character-set = utf8]

3、初始化mysql登录密码

  1. mkdir -p /opt/data/mysql
  2. mysqld --initialize --datadir=/opt/data/mysql --user=mysql
  3. cat /var/log/mysqld.log | grep "temporary password"

2023-06-23T14:24:36.418927Z 1 [Note] A temporary password is generated for root@localhost: j_urk/?#t6:Q

j_urk/?#t6:Q就是初始化登录mysql密码

4、启动mysql

  1. # 启动mysql
  2. systemctl start mysqld
  3. # mysl开机启动
  4. systemctl enable mysqld
  5. # 查看mysql启动状态
  6. systemctl status mysqld

5、修改密码

登录mysql

  1. mysql -u root -p

修改密码

  1. ALTER USER USER() IDENTIFIED BY 'Admin2023!';

修改成安全级别的密码,例如123456

修改密码策略

  1. 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 | LOW |
| validate_password_special_char_count | 1 |
+———————————————————+———-+

  1. # 密码级别设为低
  2. set global validate_password_policy=LOW;
  3. # 密码最小长度设置为6
  4. set global validate_password_length = 6;
  5. #修改密码为123456
  6. alter user 'root'@'localhost' identified by '123456';
  7. #允许root用户能远程登录
  8. grant all privileges on *.* to root@'%' identified by '123456';
  9. #刷新
  10. FLUSH PRIVILEGES;