环境说明
系统:centos7.5
软件版本:mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
安装目录:/usr/local/mysql
数据库目录:/data/mysql
数据库root密码:hc123456
A数据库:192.168.2.72 主
B数据库:192.168.2.73 从
数据库同步账号: sync
数据库sync密码: pcpaeyphmp
安装MySql
两台分别都安装
创建用户,组
创建mysql组指定组id为1200,创建mysql用户指定用户id为1200默认组mysql
groupadd -g 1200 mysql
useradd -r -g mysql -u 1200 -s /sbin/nologin mysql
创建数据目录
mysql数据库数据存储目录
datadir=/data/mysql # 数据库数据存储目录
mkdir -p $datadir
basedir=/usr/local/mysql # 数据库安装目录
关闭Selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
安装 jemalloc
配置软件安装源,安装jemalloc适合多线程下内存分配管理,减少内存碎片
rm -rf /etc/yum.repos.d/*
curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo
yum install -y jemalloc-devel
下载软件,解压
curl -OL http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz --progress
tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64/ $basedir
配置服务
cp $basedir/support-files/mysql.server /etc/init.d/mysqld
sed -i "s@^basedir=.*@basedir=$basedir@" /etc/init.d/mysqld
sed -i "s@^datadir=.*@datadir=$datadir@" /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
mysql配置文件
cat << EOF >/etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash
[mysqld]
skip-ssl
port = 3306
user = mysql
server-id = 1
bind-address = 0.0.0.0
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = $basedir
datadir = $datadir
character-set-server = utf8mb4
pid-file = $datadir/mysql.pid
init-connect = 'SET NAMES utf8mb4'
back_log = 300
#skip-networking
skip-name-resolve
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
slow_query_log = 1
long_query_time = 1
log_error = $datadir/mysql-error.log
slow_query_log_file = $datadir/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10M
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF
数据库配置优化
cp /etc/my.cnf{,.bak}
Mem=`free -m | awk '/Mem:/{print $2}'`
sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf
if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then
# 1500MB < 实际内存 <= 2500MB
sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then
# 2500MB < 实际内存 <= 3500MB
sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
elif [ ${Mem} -gt 3500 ]; then
# 3500MB < 实际内存
sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
fi
配置mysql库文件
rm -rf /etc/ld.so.conf.d/mariadb-x86_64.conf
echo "$basedir/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig
初始化数据库
添加
libjemalloc
模块,初始化数据库,启动mysql
服务
sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' $basedir/bin/mysqld_safe
$basedir/bin/mysqld --initialize-insecure --user=mysql --basedir=$basedir --datadir=$datadir
chmod 600 /etc/my.cnf
chown mysql.mysql -R $datadir
systemctl start mysqld
lsof | grep jemalloc
添加环境变量
echo -e "\n# mysqld\nexport PATH=\$PATH:$basedir/bin" >> ~/.bashrc
. ~/.bashrc
初始化root密码, 权限
mysql授权 127.0.0.1,local 表示仅本机, % 表示所有主机
RootPass=hc123456 # 数据库root密码
mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"${RootPass}\" with grant option;"
mysql -e "grant all privileges on *.* to root@'localhost' identified by \"${RootPass}\" with grant option;"
mysql -uroot -p${RootPass} -e "reset master;"
开启root远程登录权限
mysql -uroot -p${RootPass} -e "grant all privileges on *.* to root@'%' identified by \"${RootPass}\" with grant option;"
# 查看数据库用户权限
mysql -uroot -p${RootPass} -e "select user,host,authentication_string from mysql.user;"
配置防火墙
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
A数据库配置
开启二进制日志
1.开启binlog(数据库二进制日志),设置server-id,重启mysql服务
cp /etc/my.cnf{,.`date +%F`}
sed -i '14a log-bin = mysql-bin' /etc/my.cnf
sed -i 's/server-id.*/server-id = 1/g' /etc/my.cnf
sed -i '18a log-bin-index = master-bin.index' /etc/my.cnf
systemctl restart mysqld
创建同步账户,授权
2.创建同步数据的用户,授权允许192.168.2.0/24网络使用sync用户登录
mysql -uroot -p${RootPass}
create user sync;
grant replication slave on *.* to 'sync'@'192.168.2.%' identified by 'pcpaeyphmp';
flush privileges;
show master status;
exit
# MySQL [(none)]> show master status;
# 记下 binlog文件的position(偏移)和File(日志文件)的值)
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 797 | | | |
+------------------+----------+--------------+------------------+-------------------+
B数据库配置
1.配置从库只允许来自服务器线程或具有SUPER权限的数据库用户进行更新(root,sync),开启binlog(数据库二进制日志),设置server-id,重启mysql服务
cp /etc/my.cnf{,.`date +%F`}
sed -i '11a read-only' /etc/my.cnf
sed -i '15a log-bin = mysql-bin' /etc/my.cnf
sed -i 's/server-id.*/server-id = 2/g' /etc/my.cnf
sed -i '19a log-bin-index = master-bin.index' /etc/my.cnf
systemctl restart mysqld
配置连接A数据库
mysql -uroot -p${RootPass}
change master to master_host='192.168.2.72', # A数据库 IP或主机名(需解析通)
master_port=3306, # A数据库端口号
master_user='sync', # 同步账号
master_password='pcpaeyphmp', # 同步账号的密码
master_log_file='mysql-bin.000002', # A数据库执行 show master status; 获取的 File 值
master_log_pos=797; # A数据库执行 show master status; 获取的 Position 值
start slave; # 启动slave
show slave status\G; # 查看同步状态
exit
# MySQL [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.72
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 797
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
............................
# 保证以下两项状态为 Yes 连接成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证
A创建数据库
create database test_01;
show databases;
# MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_01 |
+--------------------+
B查看数据库
show databases;
# MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_01 |
+--------------------+
排错
Slave_IO_Running: # 状态不为Yes
1.数据库A与数据库B之间网络是否通畅
2.同步账号的用户权限,及是否允许数据库B登录
3.查看两端server-id(不能相同) 使用mysql命令查看 show variables like 'server_id'; 此配置项在my.cnf
4.配置从连接主时 binlog 文件的 position(偏移)和 File(日志文件)的值) 是否正确