本文安装的mysql版本为5.7,需要安装mysql 8.0以上可自行换yum文件
1、下载和安装mysql
yum -y install https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/mysql-community.repo
yum clean all
yum -y install mysql-community-server mysql-community-client
2、配置mysql
vi /etc/my.cnf
添加如下内容:
[mysqld]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server = utf8
collation-server = utf8_general_ci
init_connect = "SET NAMES 'utf8'"
explicit_defaults_for_timestamp=true
[mysql]
default-character-set = utf8
[client]
default-character-set = utf8]
3、初始化mysql登录密码
mkdir -p /opt/data/mysql
mysqld --initialize --datadir=/opt/data/mysql --user=mysql
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
# 启动mysql
systemctl start mysqld
# mysl开机启动
systemctl enable mysqld
# 查看mysql启动状态
systemctl status mysqld
5、修改密码
登录mysql
mysql -u root -p
修改密码
ALTER USER USER() IDENTIFIED BY 'Admin2023!';
修改成安全级别的密码,例如123456
修改密码策略
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 |
+———————————————————+———-+
# 密码级别设为低
set global validate_password_policy=LOW;
# 密码最小长度设置为6
set global validate_password_length = 6;
#修改密码为123456
alter user 'root'@'localhost' identified by '123456';
#允许root用户能远程登录
grant all privileges on *.* to root@'%' identified by '123456';
#刷新
FLUSH PRIVILEGES;