本篇文章为大家展示了如何搭建MySQL8和CENTOS7.6,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司主要从事成都网站设计、做网站、成都外贸网站建设公司、网页设计、企业做网站、公司建网站等业务。立足成都服务六安,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
一、环境准备
总共3个IP地址:2个物理机IP,1个VIP
MASTER:10.18.96.15
SLAVE: 10.18.96.16
VIP: 10.18.96.17
操作系统:CENTOS7.6
数据库:MYSQL8.0.20
我的硬件资源是8核16G
MYSQL8下载地址:
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.20.tar.gz
依赖下载地址:
https://mirrors.ustc.edu.cn/gnu/gmp/gmp-6.1.2.tar.xz
https://mirrors.ustc.edu.cn/gnu/mpfr/mpfr-4.0.2.tar.gz
https://mirrors.ustc.edu.cn/gnu/mpc/mpc-1.1.0.tar.gz
https://mirrors.ustc.edu.cn/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz
https://github.com/Kitware/CMake/releases/download/v3.15.2/cmake-3.15.2.tar.gz
(CMAKE如果使用YUM源安装了CMAKE3,也可以不下载了)
二、依赖安装
在安装MYSQL8之前,得先有前置准备,否则就是各种报错,无法安装
1、GCC编译安装
yum -y install epel-release
yum -y group install "Development Tools"
mysql 依赖
yum -y install ncurses-devel openssl openssl-devel bison
yum install cmake3
2、安装gcc 源码编译依赖
## 先编译gmp->mpfr->mpc
cd /usr/local/src/
编译: gmp-6.1.2
tar -xvf /opt/gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make -j $(nproc)
make install
cd ../
编译:mpfr-4.0.2
tar -xvf /opt/mpfr-4.0.2.tar.gz
cd mpfr-4.0.2
./configure --prefix=/usr/local/mpfr-4.0.2 --with-gmp=/usr/local/gmp-6.1.2
make -j $(nproc)
make install
cd ../
编译:mpc-1.1.0
tar -xvf /opt/mpc-1.1.0.tar.gz
cd mpc-1.1.0
./configure --prefix=/usr/local/mpc-1.1.0 --with-mpfr=/usr/local/mpfr-4.0.2 --with-gmp=/usr/local/gmp-6.1.2
make -j $(nproc)
make install
# 把mpfr lib 加入 ld.so.conf 不然gcc 编译报错
echo /usr/local/mpfr-4.0.2/lib >> /etc/ld.so.conf
ldconfig
编译安装gcc
cd /usr/local/src/
tar -xvf /opt/gcc-9.2.0.tar.gz
cd gcc-9.2.0
./configure --prefix=/usr/local/gcc-9.2.0 \
-enable-threads=posix \
-disable-checking \
-disable-multilib \
-enable-languages=c,c++ \
--with-gmp=/usr/local/gmp-6.1.2 \
--with-mpfr=/usr/local/mpfr-4.0.2 \
--with-mpc=/usr/local/mpc-1.1.0 \
--with-tune=generic \
--with-arch_32=x86-64
make -j $(nproc)
make install -j $(nproc)
(友情提醒,我再make编译GCC的时候,跑了1个多小时才跑完)
##备份旧 gcc 可执行文件
mv /usr/bin/gcc /usr/bin/gcc.old
mv /usr/bin/g++ /usr/bin/g++.old
mv /usr/bin/c++ /usr/bin/c++.old
mv /usr/bin/cpp /usr/bin/cpp.old
mv /usr/bin/gcov /usr/bin/gcov.old
## 创建最新gcc 执行文件软链
ln -sf /usr/local/gcc-9.2.0/bin/* /usr/bin/
## 删除lib64 目录下.py 文件不然ldconfig 报错
cp /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27-gdb.py /opt/
rm -rf /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27-gdb.py
echo /usr/local/gcc-9.2.0/lib64 >> /etc/ld.so.conf
ldconfig
## 复制libstdc++.so.6.0.27 /lib64/
cp /usr/local/gcc-9.2.0/lib64/libstdc++.so.6.0.27 /lib64/
# 创建软链 libstdc++.so.6
cd /lib64
ln -sf libstdc++.so.6.0.27 libstdc++.so.6
## 查看是否最新版本
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
编译安装cmake cd /usr/local/src/
tar -xvf /opt/cmake-3.15.2.tar.gz
cd /opt/cmake-3.15.2
./configure --prefix=/usr/local/cmake-3.15.2
gmake -j $(nproc)
gmake install -j $(nproc)
# 创建cmake 软链
ln -sf /usr/local/cmake-3.15.2/bin/cmake /bin/cmake3
三、MYSQL8安装
cd /opt/
tar xvf mysql-boost-8.0.20.tar.gz
cd /opt/mysql-8.0.20/
1、cmake
cmake3 \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
-DEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNODB_MEMCACHED=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DCOMPILATION_COMMENT="nancy edition" \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/data/mysql/tmp \
-DMYSQL_UNIX_ADDR=/data/mysql/run/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DSYSCONFDIR=/data/mysql \
-DWITH_READLINE=1 \
-DFORCE_INSOURCE_BUILD=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DCMAKE_CXX_COMPILER=/usr/local/gcc-9.2.0/bin/g++ \
-DCMAKE_C_COMPILER=/usr/local/gcc-9.2.0/bin/gcc
注意:如果你的机器没有联网,需要自己下载一个包boost_1_70_0.tar.gz放到/data/mysql/tmp目录下
下载地址:
https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
2、编译:
gmake -j $(nproc)
3、安装:
gmake install -j $(nproc)
4、创建MYSQL用户及相关目录、授权
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
mkdir /data/mysql/data/
chown mysql.mysql /data/mysql/data/
mkdir /data/mysql/run
chown mysql.mysql /data/mysql/run
mkdir /data/mysql/binlog/
chown mysql.mysql /data/mysql/binlog/
chown -R mysql.mysql /data/mysql/
5、配置文件(主库)
vi /etc/my.cnf
[mysqld]
port = 3306
user = mysql
socket = /data/mysql/run/mysql.sock
pid_file = /data/mysql/mysqld.pid
basedir = /usr/local/mysql
datadir = /data/mysql/data
tmpdir = /data/mysql/tmp
open_files_limit = 65535
server_id = 1
#默认为0,0代表区分大小写,1代表不区分大小写,以小写存储,开发要求要区分大小写
lower_case_table_names = 0
character_set_server = utf8mb4
# 允许最大连接数
max_connections = 6000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors = 10
# 默认8小时。交互等待时间和非交互等待时间
# 建议300~500s,两参数值必须一致,且同时修
interactive_timeout = 500
wait_timeout = 500
#短时间内的多少个请求可以被存在堆栈中
back_log=1024
default_storage_engine = InnoDB
#log_slave_updates = 1
#query_cache_size=1M
#*********** Logs related settings ***********
log_bin = /data/mysql/binlog/mysql-bin
log-bin-index=/data/mysql/binlog/mysql-bin.index
#binlog日志过期时间设置,单位为秒,604800=7天
binlog_expire_logs_seconds=604800
log_bin_trust_function_creators=1
#最小400,对内存占用影响大
#服务模式:从1400设置为400,内存从324M降到227M
#table_definition_cache=400
#优化参数,按事务刷盘,刷日志 [0:最快模式,1:安全模式,2:比0安全但比0要慢]
innodb_flush_log_at_trx_commit=2
# 提交1次事务刷1次,可以为n
sync_binlog=1
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
# 脏页占innodb_buffer_pool_size的比例时,触发刷脏页到磁盘
# 25%~50%
innodb_max_dirty_pages_pct=30
# 后台进程最大IO性能指标
# 默认200,如果SSD,调整为5000~20000
innodb_io_capacity=200
# 默认10M。防止高并发下,数据库受影响
innodb_data_file_path=ibdata1:1024M:autoextend
# 默认2,单位s。慢查询时间,建议0.3-0.5
long_query_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_file_per_table=1
#决定压缩程度的参数,如果你设置比较大,那么压缩比较多,耗费的CPU资源也较多;
#相反,如果设置较小的值,那么CPU占用少。默认值6,可以设置0-9#
innodb_compression_level=6
#指定在每个压缩页面可以作为空闲空间的最大比例,
#该参数仅仅应用在设置了innodb_compression_failure_threshold_pct不为零情况下,并且压缩失败率通过了中断点。
#默认值50,可以设置范围是0到75
innodb_compression_pad_pct_max=50
[mysqld_safe]
log-error=/data/mysql/data/csp-serverdb-1.err
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
6、配置文件(从库)其实和主库一样的,就server_id改一下
7、 复制启动文件
cp /opt/mysql-8.0.20/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
8、 添加mysql 到环境变量
echo export PATH=$PATH:/usr/local/mysql/bin >>/etc/profiles
source /etc/profile
echo /usr/local/mysql/lib >> /etc/ld.so.conf
ldconfig
echo PATH=$PATH:/usr/local/mysql/bin ~/.bash_profile
source ~/.bash_profile
9、初始化
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
初始化后屏幕上会打印出来root用户登录临时随机密码
10、启动
service mysqld start
或者
/etc/init.d/mysqld start
或者
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
不记得初始化密码,可以在日志中查找
/data/mysql/data/serverdb-1.err
11、修改密码:
alter user 'root'@'localhost' identified by '';
flush privileges;
(使用mysql -uroot -hlocalhost可本地免密登录,用于写keepalived脚本)
create user 'root'@'%' identified by '123456' with GRANT OPTION;
grant all privileges on *.* to 'root'@'%';(建其他普通用户也是差不多的套路,详细增减权限也可以在navicat操作)
ALTER user 'root'@'%' IDENTIFIED BY '123456'; (这个是修改)
flush privileges;
四、MYSQL主从配置
1、主库
清除一下防火墙
iptables -F
setenforce 0
create user 'repl_user'@'%' identified by '123456';
flush privileges;
grant REPLICATION SLAVE on *.* to 'repl_user'@'%';
flush privileges;
master_log_file和master_log_pos通过在主库上使用命令获得:
show master status \G;
2、从库
清除一下防火墙
iptables -F
setenforce 0
在从库上执行:
change master to
master_host='10.18.96.15',
master_port=3306,
master_user='repl_user',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=156;
start slave;
show slave status\G;
五、keepalived搭建
1、安装keepalived
cd /opt/
tar zxvf /opt/keepalived-1.4.2.tar.gz
cd /opt/keepalived-1.4.2
./configure
如果遇到以下报错
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.
解决方法:
yum -y install openssl-devel
2、编译:
make
make install
3、拷贝配置文件
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
4、配置文件
vi /etc/keepalived/keepalived.conf
global_defs {
router_id MySQL-HA2
}
vrrp_script check_run {
script "/etc/keepalived/mysql_check.sh"
interval 60
}
vrrp_sync_group VG2 {
group {
VI_1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
check_run
}
notify_master /etc/keepalived/master.sh
notify_stop /etc/keepalived/stop.sh
virtual_ipaddress {
10.18.96.17
}
}
从库一样的,就是priority 90 这里改成90
5、自动切换脚本,2个节点一样的配置
(1)
vi /etc/keepalived/mysql_check.sh
#!/bin/bash
. ~/.bash_profile
count=1
while true
do
mysql -uroot -hlocalhost -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ]
then
exit 0
else
if [ $count -gt 5 ]
then
break
fi
let count++
continue
fi
fi
done
/usr/bin/systemctl stop keepalived
(2)
vi /etc/keepalived/master.sh
#!/bin/bash
. /home/mysql/.bashrc
Master_Log_File=$(mysql -uroot -hlocalhost -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -hlocalhost -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -hlocalhost -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -hlocalhost -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
i=1
while true
do
if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
mysql -uroot -hlocalhost -e "stop slave;"
mysql -uroot -hlocalhost -e "reset slave all;"
mysql -uroot -hlocalhost -e "reset master;"
mysql -uroot -hlocalhost -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
(3)
vi /etc/keepalived/stop.sh
#!/bin/bash
. ~/.bash_profile
M_File1=$(mysql -uroot -hlocalhost -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -uroot -hlocalhost -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -uroot -hlocalhost -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -uroot -hlocalhost -e "show master status\G" | awk -F': ' '/Position/{print $2}')
i=1
while true
do
if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
然后确认MYSQL主从都已配置完成并启动
启动keepalived
systemctl start keepalived
下面这个是停止命令
systemctl stop keepalived
这样就部署完成了,其他的测试啥的(什么自动主备切换啊,VIP漂浮啊),自己搞就好了。
注意,如果是生产库中,有多套keepalived的,virtual_router_id,group,instance之类的编组更换一下就好。
如果是生产库,注意一下root用户授权
create user 'root'@'%' identified by '123456' with GRANT OPTION;
grant all privileges on *.* to 'root'@'%';
flush privileges;
还有一个问题就是,我安装完MYSQL,发现内存16G全被吃完了,手动刷新下缓存
echo 1 > /proc/sys/vm/drop_caches
然后观察一段时间,空闲内存没有再被吃完。
上述内容就是如何搭建MYSQL8和CENTOS7.6,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
名称栏目:如何搭建MYSQL8和CENTOS7.6
标题URL:http://lswzjz.com/article/jieege.html