一、安装
1.1、先删除mariadb
$ rpm -qa|grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
$ rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
$ rpm -qa|grep mariadb
1.2、centos的yum 源中默认是没有mysql的,所以我们需要先去官网下载mysql的repo源并安装
$ wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
#本地安装mysql的yum源
$ yum localinstall mysql80-community-release-el7-1.noarch.rpm
#查看/etc/yum.repos.d/路径下多了mysql文件
$ ll /etc/yum.repos.d/ |grep mysql
#yum源
$ yum clean all
$ yum makecache
#查看yum中mysql版本
$ yum repolist all | grep mysql
1.2、安装
#安装mysql
$ yum install mysql-community-server
# 会报错,执行如下:
$ rpm --checksig /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
$ gpg --export -a 3a79bd29 > 3a79bd29.asc
$ rpm --import 3a79bd29.asc
$ rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
$ rpm --checksig /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
# 会报错,执行如下
#安装mysql
$ yum install mysql-community-server
# 千万别启动
二、修改配置
2.1、修改区分大小写、链接数配置
#datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# /home/domain/mysql
datadir=/home/domain/mysql/mysql
#socket=/home/domain/mysql/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 区分0 不区分1
lower_case_table_names=1
# 最大链接数
max_connections=4000
# 最大错误数
max_connect_errors=100
innodb_buffer_pool_size=4G
innodb_flush_log_at_trx_commit=1
# 提交1次事务刷1次,可以为n
sync_binlog=1
# 默认200,如果SSD,调整为5000~20000
#innodb_io_capacity=200
# 默认10M。防止高并发下,数据库受影响
#innodb_data_file_path=ibdata1:1024M:autoextend
# 默认2,单位s。慢查询时间。建议0.1~0.5
#long_qurey_time=0.3
# 8.0默认row。记录格式,让数据安全可靠
#binlog_format=row
# 默认8小时。交互等待时间和非交互等待时间
# # 建议300~500s,两参数值必须一致,且同时修改
interactive_timeout=500
wait_timeout=500
#日志大小
innodb_log_file_size=256M
##日志缓存大小
innodb_log_buffer_size=12M
#
innodb_flush_log_at_trx_commit=2
# 设置编码格式
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
#这里确认是否起用压缩存储功能
innodb_file_per_table=1
2.2、启动服务改密码
#启动
systemctl start mysqld.service
#设置开机自启动
systemctl enable mysqld.service
#停止
systemctl stop mysqld.service
#重启
systemctl restart mysqld.service
# 查看密码
cat /var/log/mysqld.log | grep password
#使用初始密码登录
mysql -uroot -p
mysql> alter user 'root'@'localhost' identified with mysql_native_password BY '12345678';
mysql> flush privileges;
mysql> use mysql;
# 查询能访问的host,默认只能localhost访问
mysql> select user,authentication_string,host from user;
# 修改root用户能够任何IP访问,当然也能制定具体某个IP
mysql> update user set host = '%' where user = 'root';
mysql> flush privileges;
2.3、关闭服务器防火墙
# 查询防火墙状态
systemctl status firewalld
# 启动
systemctl start firewalld
# 关闭
systemctl stop firewalld
# 查看已开启端口
firewall-cmd --list-port
# 开启端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重启
firewall-cmd --reload
三、修改数据存储目录
3.1、迁移/var/lib/mysql
# 停止mysql
systemctl stop firewalld
#查看状态
systemctl status firewalld
# 迁移目录
cp -a /var/lib/mysql /home/domain/mysql
# 修改my.cnf ,注意:socket不能更改,更改后无法启动
vi /etc/my.cnf
#datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# /home/domain/mysql
datadir=/home/domain/mysql/mysql
#socket=/home/domain/mysql/mysql/mysql.sock
四、MySQL数据文件被删除,恢复方法
# 添加到my.cnf,重启即可
[mysqld]
innodb_force_recovery = 1