码迷,mamicode.com
首页 > 数据库 > 详细

CentOS7.X安装部署mysql5.7主从环境

时间:2019-11-06 18:47:49      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:hash   导入   步骤   run   ulimit   变化   停止   roc   init   

生产搭建环境:
操作系统:CentOS7.5
内存大小:32GB
硬盘:500G
主库IP:192.168.8.129 hostname:master
从库IP:192.168.8.130 hostname:slave

涉及主从都需要执行的地方,文中只写主库的操作指使,从库请自觉执行相同的命令

目录:
1、环境准备与清理
2、参数调优
3、安装部署

1、系统初始化
1.1.1. 主从库环境关闭selinux


[root@master]# sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/g‘ /etc/selinux/config
[root@master]# setenforce 0


1.1.2. 主从库系统主机时间、时区、系统语言
? 修改时区


[root@master]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


? 修改系统语言环境


[root@master]# echo ‘LANG="en_US.UTF-8"‘ >> /etc/profile && source /etc/profile


2、数据库安装
2.1 主从环境准备
2.1.1. 环境清理


[root@master]# yum list installed | grep mariadb
[root@master]# yum list installed | grep mysql
[root@master]# yum -y remove mariadb
[root@master]# yum -y remove mysql*
[root@master]# rm -rf /etc/my.cnf


2.1.2. 环境依赖包安装


[root@master]# yum -y install bzr zlib-devel gcc-c++ ncurses-devel libev make gcc autoconf automake zlib libxml libgcrypt libtool bison perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL perl-Digest-MD5 rsync perl-Data-Dumper net-tools


2.1.3. 创建用户设置

[root@master]# groupadd mysql
[root@master]# useradd mysql -g mysql -s /sbin/nologin -M

2.1.4. 配置系统内核参数


[root@master]# vi /etc/sysctl.conf
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
vm.swappiness = 0
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
kernel.shmmax = 8589934592
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.sem = 1010 129280 1010 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 4194304
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
fs.aio-max-nr = 1048576
fs.file-max = 6815744
[root@master]# /sbin/sysctl -p


2.1.5. 修改系统资源限制


[root@master]# vi /etc/security/limits.conf
mysql soft nproc 65535
mysql hard nproc 65535
mysql soft nofile 65535
mysql hard nofile 65535
mysql soft stack 10240


2.1.6. 修改环境变量


[root@master]# vi /etc/profile
if [ $USER = "mysql" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
[root@master]# source /etc/profile


2.2 主从环境安装mysql
2.2.1. 创建软件安装目录


[root@master]# mkdir -p /data/mysql/{logs,data,binlog}
[root@master]# mkdir /var/lib/mysql
[root@master]# chown -R mysql:mysql /data/
[root@master]# chown -R mysql:mysql /var/lib/mysql


2.2.2. 安装部署
? 下载MySQL Yum Repository源


[root@master]# yum -y install numactl wget
[root@master]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@master]# rpm -ivh mysql57-community-release-el7-10.noarch.rpm


? 安装MySQL(上传mysql软件包到/opt/software)


[root@master]# cd /opt/software
[root@master]# rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm mysql-community-devel-5.7.27-1.el7.x86_64.rpm mysql-community-libs-5.7.27-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.27-1.el7.x86_64.rpm mysql-community-common-5.7.27-1.el7.x86_64.rpm mysql-community-server-5.7.27-1.el7.x86_64.rpm


2.2.3. 配置数据库启动参数


[root@master]# vi /etc/my.cnf
[client]
port = 3306

[mysql]
auto-rehash

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data
port = 3306
character_set_server = utf8
explicit_defaults_for_timestamp = off
log_timestamps = system
socket = /var/lib/mysql/mysql.sock
read_only = 0
skip_name_resolve = 1
auto_increment_increment = 1
auto_increment_offset = 1
secure_file_priv =
open_files_limit = 65535
max_connections = 1000
thread_cache_size = 64
table_open_cache = 81920
table_definition_cache = 4096
table_open_cache_instances = 64
max_prepared_stmt_count = 1048576
binlog_format = row
log_bin = /data/mysql/binlog/mysql-bin
binlog_rows_query_log_events = on
log_slave_updates = on
expire_logs_days = 7
binlog_cache_size = 65536
log_bin_trust_function_creators = 1
sync_binlog = 1
slave-preserve-commit-order = ON
log_error = /data/mysql/logs/error.log
general_log = off
general_log_file = /data/mysql/logs/general.log
slow_query_log = on
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 1.000000
gtid_mode = on
enforce_gtid_consistency = on
skip_slave_start = 1
slave_parallel_type = logical_clock
slave_parallel_workers = 4
binlog_group_commit_sync_delay = 100
binlog_group_commit_sync_no_delay_count = 10
default_storage_engine = innodb
default_tmp_storage_engine = innodb
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_temp_data_file_path = ibtmp1:12M:autoextend
innodb_buffer_pool_filename = ib_buffer_pool
innodb_log_group_home_dir = /data/mysql/data
innodb_log_files_in_group = 3
innodb_log_file_size = 1024M
innodb_file_per_table = on
innodb_online_alter_log_max_size = 128M
innodb_open_files = 65535
innodb_page_size = 16k
innodb_thread_concurrency = 0
innodb_read_io_threads = 4
innodb_write_io_threads = 4
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_print_all_deadlocks = on
innodb_deadlock_detect = on
innodb_lock_wait_timeout = 20
innodb_spin_wait_delay = 128
innodb_autoinc_lock_mode = 2
innodb_io_capacity = 200
innodb_io_capacity_max = 2000
innodb_stats_auto_recalc = on
innodb_stats_persistent = on
innodb_stats_persistent_sample_pages = 20

innodb_adaptive_hash_index = on
innodb_change_buffering = all
innodb_change_buffer_max_size = 25
innodb_flush_neighbors = 1
#innodb_flush_method =
innodb_doublewrite = on
innodb_log_buffer_size = 128M
innodb_flush_log_at_timeout = 1
innodb_flush_log_at_trx_commit = 1
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 4
autocommit = 1
innodb_old_blocks_pct = 37
innodb_old_blocks_time = 1000
innodb_read_ahead_threshold = 56
innodb_random_read_ahead = OFF
innodb_buffer_pool_dump_pct = 25
innodb_buffer_pool_dump_at_shutdown = ON
innodb_buffer_pool_load_at_startup = ON


2.2.4. 启动数据库,配置服务自启动


[root@master]# systemctl start mysqld
[root@master]# systemctl enable mysqld


? 查看数据库是否已经启动,配置服务是否添加自启动成功


[root@master]# ps -ef|grep mysqld #查看运行进程
[root@master]# systemctl status mysqld #查看运行状态
[root@master]# systemctl list-unit-files |grep mysqld #查看是否自启动


2.2.5. 修改MySQL数据库root用户密码
? 获取MySQL数据库root用户的临时密码


[root@master]# cat /data/mysql/logs/error.log | grep ‘A temporary password‘
[Note] A temporary password is generated for root@localhost: #5+t+xYW+<t?
? 其中#5+t+xYW+<t?即为root用户的临时密码
#mysql -uroot -p
Enter password:
mysql> alter user ‘root‘@‘localhost‘ identified by ‘密码‘;
mysql> flush privileges;


2.2.6. 防火墙开放端口
? 根据实际要求,添加开放端口
[root@master]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
? 重新载入
#firewall-cmd --reload


2.2.7. 数据导入


[root@master]# mysql -uroot -p
Enter password:
mysql> create database sidname default character set utf8 collate utf8_bin;
mysql> grant select,insert,update,delete,create,execute on sidname.* to ‘username‘@‘%‘ identified by ‘密码‘;
mysql> flush privileges;
还原数据库
[root@master]# mysql -uroot -p‘passwd‘ sidname < /tmp/XXX.sql


2.3 主从数据库配置
2.3.1 主库环境配置
? 主库的配置分8个步骤(注意操作的连接窗口执行对应的命令)
Shell窗口1:
1、授权给从机服务器(根据实际需要配置从机登录用户名称slave与密码)


[root@master]# mysql -uroot -p
Enter password:
mysql> GRANT REPLICATION SLAVE ON . to ‘slave‘@‘192.168.8.130‘ identified by ‘密码‘;
mysql> FLUSH PRIVILEGES;


2、修改主库配置文件并重启数据库(根据实际添加server-id与binlog-do-db参数,其中server-id是MySQL 在主从环境下的唯一标志符,给个任意数字,注意不能和局域网内其他机器重复;binlog-do-db是要同步的数据库名,当从机连上主机后,只有这里配置的数据库才会被同步,其他的不会被同步)


[root@master]# vi /etc/my.cnf
[mysqld]
server-id = 129
binlog-do-db = sidname
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
binlog-ignore-db = sys
重启数据库服务
[root@master]# systemctl stop mysqld
[root@master]# systemctl start mysqld


3、确认同步的账号已经添加及查看timeout时间以便足够用于备份数据库


[root@master]# mysql -uroot -p
Enter password:
mysql> select user,host from mysql.user;
mysql> show variables like ‘%timeout%‘;


4、对数据库锁表只读
mysql> flush tables with read lock;
5、查看当前主库状态并记录当前二进制日志名和偏移量master_log_file(File)与master_log_pos(Position)值
mysql> show master status;
技术图片
6、在当前shell连接不断开的情况,再新建shell窗口2连接主库,操作备份数据库并再次确认当前二进制日志名和偏移量master_log_file(File)与master_log_pos(Position)值是否有变化
Shell窗口2:



[root@master]# mysqldump --set-gtid-purged=off -uroot -p -A -B |gzip > /tmp/mysqlbackupdate +%F.sql.gz
[root@master]# mysql -uroot -p -e "show master status"


7、回到shell窗口1上执行解锁数据库:
mysql> unlock tables;
8、将/tmp/mysqlbackupdate +%F.sql.gz下载或通过scp等方式拷贝到从库机器上
至此,完成主库的【锁表】---【备份全库+记录日志名与偏移量】---【表解锁】操作

3.2 从库配置
? 从机的配置分四个步骤
1、修改从库配置文件(根据实际添加server-id参数,server-id是MySQL 在主从环境下的唯一标志符,给个任意数字,注意不能和局域网内其他机器重复)


[root@slave]# vi /etc/my.cnf
[mysqld]
server-id = 130
read_only = 1 #从库开启只读模式
master_info_repository = table
relay_log_info_repository = table
replicate-do-db = sidname
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = sys
重启数据库服务
[root@slave]# systemctl stop mysqld
[root@slave]# systemctl start mysqld


2、登录从库,并将主库备份的数据库解压


[root@slave]# cd /tmp
[root@slave]# gzip -d mysqlbackupdate +%F.sql.gz


3、如果从库已经正在运行同步,需要先停止同步


[root@slave]# mysql -uroot -p -e "stop slave;"
4、操作还原数据库
[root@slave]# mysql -uroot -p < /tmp/mysqlbackupdate +%F.sql


5、使用命令来配置主从库同步(这里配置了主库地址192.168.8.129、端口3306以及从库登录主库的用户名slave和密码密码,注意master_log_filet(File)和master_log_pos(Position)两个参数要和 master 中的保持一致)


mysql> change master to master_host=‘192.168.8.129‘,master_port=3306,master_user=‘slave‘,master_password=‘密码‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=234358702,master_connect_retry=30;


6、启动 slave 进程并查看从机状态


mysql> start slave;
mysql> show slave status\G;
mysql> show databases;


查看 slave 的状态主要是下面两项值都要为为 YES,则表示配置正确
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
技术图片
至此,配置完成,主机创建库,添加数据,从机会自动同步。
? 主库查看集群状态:


mysql> show master status\G;
mysql> show slave hosts;
? 显示主从当前所有连接的工作状态
mysql> show processlist \G;


CentOS7.X安装部署mysql5.7主从环境

标签:hash   导入   步骤   run   ulimit   变化   停止   roc   init   

原文地址:https://blog.51cto.com/8355320/2448056

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!