王海庆的云笔记

CentOS7采用yum方式安装mysql5.7(修改存储路径)


关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
sed -i -e  's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0


换成阿里云的源

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

yum clean all
yum makecache fast -y


不校验

sed -i 's#gpgcheck=1#gpgcheck=0#g'  /etc/yum.repos.d/*


安装

curl -L -o mysql57-community-release-el7-11.noarch.rpm http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum -y install mysql57-community-release-el7-11.noarch.rpm
yum install -y mysql-community-server


创建文件夹

mkdir -p /data/mysql


追加配置文件/etc/my.cnf

cat >>  /etc/my.cnf  << "EOF"

character-set-server=utf8 
collation-server=utf8_general_ci 
#character-set-server=utf8mb4
#collation-server=utf8mb4_general_ci 
lower_case_table_names=1 
max_connections=10240
open_files_limit=10240
max_connect_errors=10240
max_allowed_packet=10M
port=3306

# 设置密码的验证强度等级,这里开启这个参数会报错
# validate_password_policy=LOW

# zkxy使用此字段
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
socket=/data/mysql/mysql.sock

EOF


修改如下路径/etc/my.cnf

sed -i 's#^datadir.*#datadir=/data/mysql#g'  /etc/my.cnf
sed -i 's#^socket.*#socket=/data/mysql/mysql.sock#g'  /etc/my.cnf


启动服务

systemctl restart mysqld 
systemctl enable mysqld 
systemctl status mysqld


修改密码

# 在日志中获取密码
grep 'temporary password' /var/log/mysqld.log

# 登录mysql,密码是上一步日志获取的
mysql -uroot -p

# 修改root密码
ALTER user root@localhost identified by 'Star*1234';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' identified by 'Star*1234';

# 刷新立即生效
flush privileges;


如果mysql启动失败,可能是有哪位没有关闭防火墙和selinux,具体可以查看mysql的日志

tail -n500 /var/log/mysqld.log|grep -E 'Warning|ERROR'


文章最后更新时间: 2022-02-14 16:32:41