centos7.9安装mysql5.7完整版
1. 更新yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls -lh
#备份镜像源
[root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.backup
#下载阿里云centos7镜像
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@jztserver01 yum.repos.d]# yum clean all
[root@jztserver01 yum.repos.d]# yum makecache
[root@jztserver01 yum.repos.d]# yum update
2. 下载软件
#下载软件
[root@localhost software]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
#查看文件
[root@localhost software]# ls -lh
-rw-r--r--. 1 root root 545M Nov 8 13:39 mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
#切换目录, 创建目录
[root@localhost sqyr]# cd local/
[root@localhost local]# mkdir mysql-5.7.43
#复制安装包到指定目录
[root@localhost local]# cp /home/sqyr/software/mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar /home/sqyr/local/mysql-5.7.43/
3. 监测软件
[root@localhost software]# ps -ef | grep mysql
[root@localhost software]# netstat -anp | grep 3306
[root@localhost software]# whereis mysql
#rpm检查是否安装. 若安装执依次执行卸载
[root@localhost software]# rpm -qa |grep -i mysql
[root@localhost software]# rpm -ev mysql-community-server-5.7.43-1.el7.x86_64 --nodeps
[root@localhost software]# rpm -ev mysql-community-client-5.7.43-1.el7.x86_64 --nodeps
[root@localhost software]# rpm -ev mysql-community-common-5.7.43-1.el7.x86_64 --nodeps
[root@localhost software]# rpm -ev mysql-community-libs-5.7.43-1.el7.x86_64 --nodeps
#是否安装mariadb
[root@localhost software]# rpm -qa |grep -i mariadb
#如果存在则先停止和禁用服务, 在卸载
[root@localhost software]# systemctl stop mariadb
[root@localhost software]# systemctl disabled mariadb
[root@localhost software]# yum remove mariadb
[root@localhost software]#
#是否mysql相关文件,依次执行删除文件
[root@localhost software]# find / -name mysql
[root@localhost software]# rm -rf *目录文件
4. 安装软件
#安装环境
[root@localhost mysql-5.7.43]# yum install gcc net-tools pcre-devel zlib zlib-devel openssl openssl-devel libaio*
[root@localhost mysql-5.7.43]# yum install -y perl-Module-Install.noarch
#解压安装包后,删除安装包
[root@localhost mysql-5.7.43]# tar -xvf mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
[root@localhost mysql-5.7.43]# rm -rf mysql-5.7.43-1.el7.x86_64.rpm-bundle.tar
#检查是否安装, 若安装则依次卸载
[root@localhost mysql-5.7.43]# rpm -qa |grep -i mysql
[root@localhost mysql-5.7.43]# rpm -ev mysql-community-server-5.7.43-1.el7.x86_64 --nodeps
[root@localhost mysql-5.7.43]# rpm -ev mysql-community-client-5.7.43-1.el7.x86_64 --nodeps
[root@localhost mysql-5.7.43]# rpm -ev mysql-community-common-5.7.43-1.el7.x86_64 --nodeps
[root@localhost mysql-5.7.43]# rpm -ev mysql-community-libs-5.7.43-1.el7.x86_64 –nodeps
#监测是否安装mariadb
[root@localhost mysql-5.7.43]# rpm -qa |grep -i mariadb
[root@localhost mysql-5.7.43]# systemctl disabled mariadb
[root@localhost mysql-5.7.43]# yum remove mariadb*
#检查mysql相关文件
[root@localhost mysql-5.7.43]# find / -name mysql
[root@localhost mysql-5.7.43]# rm -rf *目录文件
#采用yum安装mysql
[root@localhost mysql-5.7.43]# rpm -ivh mysql-community-common-5.7.43-1.el7.x86_64.rpm
[root@localhost mysql-5.7.43]# rpm -ivh mysql-community-libs-5.7.43-1.el7.x86_64.rpm
[root@localhost mysql-5.7.43]# rpm -ivh mysql-community-client-5.7.43-1.el7.x86_64.rpm
[root@localhost mysql-5.7.43]# rpm -ivh mysql-community-server-5.7.43-1.el7.x86_64.rpm
[root@localhost mysql-5.7.43]# rpm -qa|grep -i mysql
#执行启动mysql
[root@localhost mysql-5.7.43]# systemctl start mysqld
#查看mysql是否启动
[root@localhost mysql-5.7.43]# ss -tnl
[root@localhost mysql-5.7.43]# ps -ef | grep mysql
#查询初始化密码
[root@localhost mysql-5.7.43]# cat /var/log/mysqld.log | grep password
#mysql初始化登录, 回车输入密码
[root@localhost mysql-5.7.43]# mysql -u root -p
#登录成功后,依次执行以下命令然后eixt; 密码安全等级后面要求更改
mysql> SET GLOBAL validate_password_policy='LOW';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '填写密码';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '填写密码' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '填写密码' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> SET GLOBAL validate_password_policy='MEDIUM';
mysql> FLUSH PRIVILEGES;
mysql> exit;
#退出后,停止服务
[root@localhost mysql-5.7.43]# systemctl stop mysqld
#创建安装目录
[root@localhost mysql-5.7.43]# mkdir -p install/data
#拷贝源数据文件到新的数据文件位置
[root@localhost mysql-5.7.43]# cp -fa /var/lib/mysql/* /home/sqyr/local/mysql-5.7.43/install/data/
#修改数据目录权限
[root@localhost mysql-5.7.43]# chown -R mysql:mysql /home/sqyr/local/mysql-5.7.43/install/
#切换目录, 删除源数据文件
[root@localhost mysql-5.7.43]# cd /var/lib/
[root@localhost lib]# mv mysql mysql_bak
#切换目录, 配置日志文件和目录权限
[root@localhost lib]# cd /home/sqyr/local/mysql-5.7.43/install/data/
[root@localhost data] mkdir logs
[root@localhost data] cd logs
[root@localhost logs]# touch mysql-slow.log
[root@localhost logs]# chmod 775 mysql-slow.log
[root@localhost logs]# touch mysqld.log
[root@localhost logs]# chmod 775 mysqld.log
[root@localhost data]# chown -R mysql:mysql /home/sqyr/local/mysql-5.7.43/install/data/logs/
#切换安装目录, 复制errmsg.sys到该目录
[root@localhost mysql-5.7.43]# cd /home/sqyr/local/mysql-5.7.43/install/
[root@localhost mysql-5.7.43]# mkdir -p share/mysql/
[root@localhost mysql-5.7.43]# cp /usr/share/mysql/english/errmsg.sys /home/sqyr/local/mysql-5.7.43/install/share/mysql/
#切换目录配置插件validate_password.so
[root@localhost mysql-5.7.43]# cd install/
[root@localhost install]# mkdir -p lib64/mysql/plugin
[root@localhost install]# cp /lib64/mysql/plugin/validate_password.so /home/sqyr/local/mysql-5.7.43/install/lib64/mysql/plugin/
#配置etc/my.cnf文件内容如下
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M[mysqld]
# 基础目录
basedir=/home/sqyr/local/mysql-5.7.43/install/
# 数据目录
datadir=/home/sqyr/local/mysql-5.7.43/install/data
# 通讯链接文件
socket=/home/sqyr/local/mysql-5.7.43/install/data/mysql.sock
# 端口号
port=3306
# 绑定地址
bind-address=0.0.0.0
# 符号链接
skip-name-resolve
# 符号连接的使用被禁用
symbolic-links=0
# 默认编码
character-set-server=utf8
# 默认时区
default-time_zone='+8:00'
# 默认采用InnoDB存储引擎
default-storage-engine=INNODB
# 时间格式
explicit_defaults_for_timestamp=true
# 默认库名表名保存为小写, 不区分大小写
lower_case_table_names=1
# 每执行一个事务都强制写入磁盘
sync_binlog=1# 使用独立表空间
innodb_file_per_table=1
# 数据缓冲区大小,如果是单实例且绝大多数是InnoDB引擎表的话,可考虑设置为物理内存的50% ~ 70%左右;
innodb_buffer_pool_size=2G
# innodb缓冲池块大小
innodb_buffer_pool_chunk_size=128M
# innodb缓冲池实例数
innodb_buffer_pool_instances=8
# 如果要求数据不能丢失,为了数据安全,那么两个都设为1。
innodb_flush_log_at_trx_commit=1
# 事务日志大小
innodb_log_file_size=256M
# 设置2,基本可满足90%以上的场景;
innodb_log_files_in_group=2
# 日志缓冲区大小
innodb_log_buffer_size=4M
# 如果库里的表特别多的情况,请增加此值
innodb_open_files=2048
# mysql的数据文件,千万不要用默认的10M,否则在有高并发事务时,会受到不小的影响
#innodb_data_file_path=ibdata1:12m;ibdata2:1g:autoextend# 服务器最大连接数
max_connections=1024
# 服务器验证密码超过1000次拒绝连接
max_connect_errors=1000
# 消息缓冲区会用到该列,该值太小则会在处理大包时产生错误。如果使用大的text,BLOB列,必须增加该值
max_allowed_packet=256M
# 排序使用的缓冲大小,高并发的情况下, 需要减小此值到64K-128K
sort_buffer_size=4M
# 若果多表连接需求大,则这个值要设置大一点
join_buffer_size=6M
# 读缓冲区大小
read_buffer_size=3M
# 随机读缓冲区大小设置
read_rnd_buffer_size=6M
# 索引块的缓冲区大默认16M
key_buffer_size=128M# 决定是否缓存查询结果。这个变量有三个取值:0,1,2,分别代表了off、on、demand。
query_cache_type=0
# 在高并发,写入量大的系统,建义把该功能禁掉
query_cache_size=64M
# 指定单个查询能够使用的缓冲区大小,缺省为1M
query_cache_limit=2M
# 表描述符缓存大小,可减少文件打开/关闭次数,一般max_connections*2。
table_open_cache=2048
table_definition_cache=2048
# 设置临时表最大值,不要设置过大,否则容易导致OOM发生
tmp_table_size=128M
max_heap_table_size=32M
# 服务器能够同时打开的文件数量上限
open_files_limit=65535
# 跳过指定error no类型的错误
slave-skip-errors=1114# 数据读取超时时间
net_read_timeout=10800
# 数据写入超时时间
net_write_timeout=10800
# 服务器关闭非交互连接之前等待活动的秒数
wait_timeout=86400
# 服务器关闭交互式连接前等待活动的秒数
interactive_timeout=86400
# 优化mysql的线程缓存,服务器线程缓存这个值表示可以重新利用保存在缓存中线程的数量
thread_cache_size=64# 控制binlog日志文件保留时间,超过时间会删除
expire_logs_days=7
# 开启慢日志查询
slow_query_log=ON
# 慢查询的阈值为3秒
long_query_time=3
# 输出慢日志文件
slow_query_log_file=/home/sqyr/local/mysql-5.7.43/install/data/logs/mysql-slow.log
# 一般有两种形式,FILE和TABLE,默认FILE; TABLE时mysql库的slow_log表中
log_output=FILE,TABLE
# 用来缓存批量插入数据的时候临时缓存写入数据。如果有大量数据插入可提高此参数值,默认是8M
bulk_insert_buffer_size=64M
# MyISAM设置恢复表之时使用的缓冲区的尺寸,默认为8M,最小值4k。当在REPAIR TABLE 或用 CREATE INDEX 创建索引或ALTER TABLE过程中对MyISAM索引排序时分配的缓冲区
myisam_sort_buffer_size=32M
#对应errmsg.sys目录和文件位置
lc-messages-dir=/home/sqyr/local/mysql-5.7.43/install/share/mysql# 设置log-error日志文件
log-error=/home/sqyr/local/mysql-5.7.43/install/data/logs/mysqld.log
# 设置pid-file进程文件
pid-file=/var/run/mysqld/mysqld.pid
# 设置mysql模式
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER[client]
socket=/home/sqyr/local/mysql-5.7.43/install/data/mysql.sock
default-character-set=utf8[mysql]
socket=/home/sqyr/local/mysql-5.7.43/install/data/mysql.sock
default-character-set=utf8
#备份源文配置文件, 并覆盖新文件
[root@localhost data]# cp /etc/my.cnf /etc/my.cnf_bak
[root@localhost data]# cp /home/sqyr/software/my.cnf /etc/
#配置selinux, 要重启才生效
[root@localhost data]# vi /etc/sysconfig/selinux
SELINUX=disabled
#设置临时selinux
[root@localhost data]# setenforce 0
[root@localhost data]# getenforce
#执行mysql启动
[root@localhost data]# systemctl start mysqld
#防火墙端口配置
[root@localhost mysql-5.7.43]# firewall-cmd --permanent --add-port=3306/tcp
[root@localhost mysql-5.7.43]# firewall-cmd --reload
[root@localhost mysql-5.7.43]# firewall-cmd --zone=public --list-ports
[root@localhost mysql-5.7.43]# systemctl restart firewall
[root@localhost mysql-5.7.43]# systemctl status firewall
#设置mysql自启动
[root@localhost mysql-5.7.43]# systemctl enable mysqld.service
[root@localhost mysql-5.7.43]# systemctl list-unit-files | grep mysqld.service