mysql5.7的主从切换

主库: master 10.11.0.211
从库: slave 10.11.0.210 目标:主从切换,将slave切换为master,master切换为slave 一、使用innobackup进行主从同步设置 # 应用的连接数据库的账号配置
grant select,insert,update,delete on task.* to task_user@"%" identified by "cmslU6WFkX2pBylwINy2T"; # 修改root密码
update mysql.user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root'; # 设置主从同步:
# 备份主库
innobackupex --defaults-file="/etc/my.cnf" --user=root -p'root' --socket=/tmp/mysql.sock --apply-log --use-memory=1G /opt/--06_15--
# 应用事务
innobackupex --defaults-file="/etc/my.cnf" --user=root --socket=/tmp/mysql.sock --copy-back /data/--06_15--/ # 配置主从账号
grant replication slave,reload,super on *.* to rep@'%' identified by 'wsdb123'; # 记录主库的同步位置
[root@node1 data]# cat --06_15--/xtrabackup_binlog_info
mysql_bin. # 设置主从同步
change master to master_host='10.11.0.211',master_user='rep',master_password='wsdb123',master_log_file='mysql_bin.000003',master_log_pos=; 二、进行主从切换操作: .锁定主数据写操作: master:
mysql> flush tables with read lock; 2然后修改从数据库为主要数据库: > 保证所有从数据库都已经执行了relay log中的全部更新,在从服务器中执行stop slave io_thread,用show processlist检查,查看状态是否是Has read all relay log,表示更新完成.
slave:
mysql>stop slave;
mysql>stop slave io_thread; mysql>show processlist\G
*************************** . row ***************************
Id:
User: system user
Host:
db: NULL
Command: Connect
Time:
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL > 在从服务器上执行stop slave,reset master命令,重置成主数据库 mysql>stop slave; mysql>reset master; mysql>reset slave all; -- 清除同步信息 > 删除新的主服务器数据库目录中的master.info和relay-log.info文件,否则下次重启时还会按照从服务器来启动,
关闭新主库的my.cnf配置 read_only > 在新的主库中查看binlog日志
mysql> show master status\G; mysql> show master status\G
*************************** . row ***************************
File: mysql_bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 重新设置主从同步,
附切换从数据库命令: change master to master_host='10.11.0.210',master_user='rep', master_password='wsdb123', master_log_file='mysql_bin.000001', master_log_pos=;
start slave; >从服务器上检测是否复制正常(Slave_IO_Running: Yes && Slave_SQL_Running: Yes) mysql> start slave;
Query OK, rows affected (0.03 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.11.0.210
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql_bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql_bin. 解锁定原主数据写操作: mysql> unlock tables;
Query OK, rows affected (0.00 sec) #修改原主库的my.cnf 打开read_only参数 进行相应的应用检查 数据库的测试:
主库创建表,并插入数据:
MySQL [wanxing]> use wanxing; Database changed
MySQL [wanxing]> show tables;
+-------------------+
| Tables_in_wanxing |
+-------------------+
| users |
+-------------------+
row in set (0.00 sec) MySQL [wanxing]> create table itpart(id int,name varchar()) ;
Query OK, rows affected (0.01 sec) MySQL [wanxing]> insert into itpart(,'it engineer'); 从库查询数据是否同步:
mysql> select * from itpart;
+------+-------------+
| id | name |
+------+-------------+
| | it engineer |
+------+-------------+
row in set (0.01 sec)

生产环境切换记录:

主库和新主库都开启screen 窗口进行操作,避免网络中断引起操作异常
screen -S changemaster 准备工作:停止数据库的写入程序
停止监控 .锁定老的主库写操作: master:
flush tables with read lock;
set global read_only=on; show variables like 'read_only'; show master status\G; .然后修改从数据库为主要数据库: > 保证所有从数据库都已经执行了relay log中的全部更新,在从服务器中执行stop slave io_thread,用show processlist检查,查看状态是否是Has read all relay log,表示更新完成 slave:
mysql>stop slave;
mysql>stop slave io_thread; mysql>show processlist\G
*************************** . row ***************************
Id:
User: system user
Host:
db: NULL
Command: Connect
Time:
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL 在从服务器上执行stop slave,reset master命令,重置成主数据库 mysql>stop slave; mysql>reset master; mysql>reset slave all; # 打开可写操作
set global read_only=off; > 删除新的主服务器数据库目录中的master.info和relay-log.info文件,否则下次重启时还会按照从服务器来启动,
关闭新主库的my.cnf配置
read_only 注释掉 #### 修改/etc/hosts配置,进行 # db
172.16.0.239 inf.nei.prod.mysql.eus > 在新的主库中查看binlog日志 mysql> show master status\G
*************************** . row ***************************
File: mysql_bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 重新设置主从同步,
附切换从数据库命令: # 日志标记
change master to master_host='10.11.0.210',master_user='rep', master_password='wsdb123', master_log_file='mysql_bin.000001', master_log_pos=;
start slave; # 从库中执行
mysql> change master to master_host='172.16.0.239' ,master_user='rep',master_password='wsdb123',master_auto_position=;
Query OK, rows affected, warnings (0.30 sec) mysql> start slave;
Query OK, rows affected (0.02 sec) >从服务器上检测是否复制正常(Slave_IO_Running: Yes && Slave_SQL_Running: Yes) mysql> start slave;
Query OK, rows affected (0.03 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.11.0.210
Master_User: rep
Master_Port:
Connect_Retry:
Master_Log_File: mysql_bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql_bin. 解锁定原主数据写操作: mysql> unlock tables;
Query OK, rows affected (0.00 sec) 注意:
.修改原主库的my.cnf 打开read_only参数,和relay-log相关参数
relay-log = mysql-relay-bin
relay-log-index = relay.index .注意新主库的read_only参数是动态修改的,一定要修改my.cnf为off,或者直接删掉,避免重启后为on,应用不可写 进行相应的应用检查 后续工作:
.数据库备份并且做主从同步
.从库上做冷备份 添加监控 主库:8核32G配置参考 [:~]# cat /etc/my.cnf

# Example MySQL config file for medium systems.
# 8核 16G
# This is for a system with little memory (16G) where MySQL plays
[client]
#password       = your_password
port                                                        = 3306
socket                                                      = /tmp/mysql.sock # The MySQL server
[mysqld]
user                                                        = mysql
port                                                        = 3306
bind-address                                                = 0.0.0.0
socket                                                      = /tmp/mysql.sock
datadir                                                     = /data/mysql_data
pid-file                                                    = /data/mysql_data/mysql.pid
basedir                                                     = /usr/local/mysql
tmpdir                                                      = /tmp #此开关默认为NULL,即不允许导入导出。
#secure-file-priv                                           = /opt/upload #-------------------------------gobal variables------------------------#
#默认关闭,涉及到timestamp类型的列自动更新的问题
explicit_defaults_for_timestamp                 = 1
###transaction_write_set_extraction              = XXHASH64  #以便在server收集写集合的同时将其记录到二进制日志。并且是行更改后的唯一标识此标识将用于检测冲突。
###loose-group_replication_group_name            = 'ce9be252-2b71-11e6-b8f4-00212889f856' #组的名字可以随便起,但不能用主机的GTID
###loose-group_replication_start_on_boot         = off  #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。
###loose-group_replication_bootstrap_group       = off #同上
###loose-group_replication_local_address         = '192.168.1.88:33071' #写自己主机所在IP
###loose-group_replication_group_seeds           ='192.168.1.88:33071,192.168.1.89:33071,192.168.1.90:33071'
###loose-group_replication_single_primary_mode   = off  #关闭单主模式的参数
###loose-group_replication_enforce_update_everywhere_checks = on #开启多主模式的参数 skip-external-locking
skip-name-resolve
skip-ssl #memory is 16G
key_buffer_size                                             = 16M
table_open_cache                                            = 2048
table_definition_cache                                      = 1024
sort_buffer_size                                            = 4M
net_buffer_length                                           = 32K
read_buffer_size                                            = 4M
read_rnd_buffer_size                                        = 16M open_files_limit                                            = 10000
thread_cache_size                                           = 400
query_cache_type                                            = 0
query_cache_size                                            = 32M
max_write_lock_count                                        = 300
wait_timeout                                                = 28800
interactive_timeout                                         = 28800
net_read_timeout                                            = 1200
net_write_timeout                                           = 1200 max_connections                                             = 800
max_user_connections                                        = 750
max_connect_errors                                          = 10000
max_allowed_packet                                          = 256M
back_log                                                    = 2048
log_timestamps                                              = system
performance_schema                                          = OFF
character_set_server                                        = utf8mb4 ##当链接数耗尽后,通过设置别用端口,让root可以登录
extra_max_connections                                       = 2
extra_port                                                  = 13306 ###让mysql不区分大小写敏感
lower_case_table_names                                      = 1 #explicit_defaults_for_timestamp                            = 1 #----------------Myisam--------------------------------#
myisam_recover_options                                      = DEFAULT
bulk_insert_buffer_size                                     = 32M
myisam_sort_buffer_size                                     = 64M
myisam_max_sort_file_size                                   = 256M
myisam_repair_threads                                       = 1 #if the query is exec time great than 2 seconds, the query will log to slow log if slowlog is enabled.
long_query_time                                             = 3
slow_query_log                                              = On
slow-query-log-file                                         = /data/mysql_data/slow.log
show_compatibility_56                                       = on # Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking #----------------------------MySQL Log----------------#
# Replication Master Server (default)
# binary logging is required for replication
log-bin                                                     = mysql-bin
expire_logs_days                                            = 10
log_error                                                   = error.log
log_error_verbosity                                         = 1
log_warnings                                                = 1 # binary logging format - mixed recommended
binlog_format                                               = row
relay-log                                                   = mysql-relay-bin
relay-log-index                                             = relay.index
# required unique id between 1 and 2^32 - 1
server-id                                                   = 239
#sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
#sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
sync_binlog                                                 = 1
log_slave_updates                                           = 1
#binlog_checksum                                            = NONE
#slave_skip_errors = 1062,1032 #------------------------------replicate--------------#
#排除不需要同步的库表
#replicate-ignore-db                                        = mysql
#replicate-ignore-db                                        = sys
replicate-ignore-db                                         = information_schema
replicate-ignore-db                                         = performance_schema
replicate-ignore-db                                         = undolog
replicate-ignore-db                                         = for_nagios
replicate-ignore-db                                         = undolog #replicate_wild_ignore_table                                = mysql.%
#replicate_wild_ignore_table                                = sys.%
replicate_wild_ignore_table                                 = information_schema.%
replicate_wild_ignore_table                                 = performance_schema.%
replicate_wild_ignore_table                                 = undolog.%
replicate_wild_ignore_table                                 = for_nagios.%
replicate_wild_ignore_table                                 = undolog.% #主主复制需要开启
#auto_increment_offset= 2
#auto_increment_increment= 2 #GTID模式复制,需要开启如下
gtid_mode                                                  = ON
enforce_gtid_consistency                                   = ON #并发复制
slave-parallel-type                                         = LOGICAL_CLOCK
slave-parallel-workers                                      = 2
master_info_repository                                      = TABLE
relay_log_info_repository                                   = TABLE
relay_log_recovery                                          = ON #跳过slave进程启动参数
skip-slave-start #如果实例为从库,则需要设置为on
#read_only                                                   = off #skip-grant-tables #--------------------------------------------------------innoDB------------#
innodb_rollback_on_timeout
# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir                                        = /data/mysql_data
innodb_data_file_path                                       = ibdata1:1G;ibdata2:1G:autoextend
innodb_log_group_home_dir                                   = /data/mysql_data
innodb_undo_directory                                       = /data/mysql_data/undolog/
innodb_undo_logs                                            = 128
innodb_undo_tablespaces                                     = 3 # You can set .._buffer_pool_size up to 50 - 80 %
#innodb_use_sys_malloc = 0
#innodb_page_size = 8192
innodb_buffer_pool_size                                     = 12G
innodb_buffer_pool_instances                                = 1
#innodb_additional_mem_pool_size = 8M # Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size                                        = 256M
innodb_log_buffer_size                                      = 64M
innodb_log_files_in_group                                   = 3
#每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认
innodb_flush_log_at_trx_commit                              = 1
innodb_lock_wait_timeout                                    = 120
#启用独立表空间
innodb_file_per_table                                       = 1 #CPU是1颗8核的,那么可以设置
innodb_read_io_threads                                      = 8
innodb_write_io_threads                                     = 8 #默认是0,则表示没有并发线程数限制,所有请求都会直接请求线程执行,当并发用户线程数量小于64,建议设置innodb_thread_concurrency=0,在大多数情况下,最佳的值是小于并接近虚拟CPU的个数
innodb_thread_concurrency                                   = 12
innodb_max_dirty_pages_pct                                  = 75
innodb_flush_method                                         = O_DIRECT innodb_purge_threads                                        = 10
innodb_large_prefix                                         = 1 #参数待测试
#innodb_io_capacity                                         = 20000
#innodb_io_capacity_max                                     = 40000 #根据CPU核心数来设定
thread_pool_size                                            = 8
#thread_handling = pool-of-threads
thread_pool_oversubscribe                                   = 24 #thread_handling                             = pool-of-threads
thread_pool_stall_limit                                     = 100
thread_pool_max_threads                                     = 30 #解释: 在启动时把热数据加载到内存。
innodb_buffer_pool_load_at_startup                          = 1
#解释: 在关闭时把热数据dump到本地磁盘
innodb_buffer_pool_dump_at_shutdown                         = 1 ##默认是8M, 如果一次insert数据量比较多的话, 可以适当增加
innodb_autoextend_increment                                 = 32 [mysqldump]
quick
max_allowed_packet                                          = 512M [mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates [myisamchk]
#key_buffer_size = 20M
#sort_buffer_size = 20M
key_buffer_size                                             = 200M
sort_buffer_size                                            = 200M
read_buffer                                                 = 2M
write_buffer                                                = 2M [mysqld_safe]
#控制文件打开数的show global status like 'open%file%';比较合适的设置:Open_files / open_files_limit * 100% <= 75%
open-files-limit                                            = 65535
log-error                                                   = /data/mysql_data/error.log [mysqlhotcopy]
interactive-timeout #启用存储过程、触发器的权限,但是会不适合于主从复制机制,会导致数据的不一致
log_bin_trust_function_creators                    = 1

mysql5.7的主从切换的更多相关文章

  1. mysql主从备份、主从切换的例子

    指定binlog(因为时通过binlog实现数据同步的) 配置完后重启数据库服务,用show master status可以看到Master信息. StepB: 在SerB的my.cnf中指定 [ht ...

  2. mycat读写分离与主从切换

    1, 分库分表的优缺点.以及为什么分表方式无法成为主流? 分表:在台server上,长处是易维护,相似表分区.缺点是在一台dbserver上.无法分担IO.负载集中. 分库:在多台server上,长处 ...

  3. Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接

    上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 ​一.步骤如下: 1.配 ...

  4. redis主从配置及主从切换

    环境描述: 主redis:192.168.10.1 6379从redis:192.168.10.2 6380 一.主从配置 1.将主从redis配置文件redis.conf中的aemonize no ...

  5. mysql主从切换

    mysql 主从切换 主停,从做主步骤如下: 1 确认从服务器已经完成所有同步操作:stop slave io_thread show processlist 直到看到状态都为:xxx has rea ...

  6. Sentinel-Redis高可用方案(二):主从切换

    Redis 2.8版开始正式提供名为Sentinel的主从切换方案,Sentinel用于管理多个Redis服务器实例,主要负责三个方面的任务:     1. 监控(Monitoring): Senti ...

  7. redis的主从复制,读写分离,主从切换

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. redis提供了一个master,多个s ...

  8. redis主从配置及主从切换 转

    redis主从配置及主从切换 转自 http://blog.sina.com.cn/s/blog_67196ddc0101h8v0.html (2014-04-28 17:48:47) 转载▼   分 ...

  9. mysql5.5 Replication 主从同步

    mysql5.5 Replication 主从同步 ------------------[主]------------------[mysqld]server-id=1 log-bin=mysql-b ...

随机推荐

  1. IDEA集成jacoco

    穷乡僻壤的人犯罪率低,不是因为他们高尚,而是因为没有选择:没有选择就不会有痛苦. --<黑冰·郭小鹏> 参考资料:https://www.jacoco.org/jacoco/trunk/d ...

  2. 多态典型用例之virtual

    多态典型用例之virtual 参考:https://www.cnblogs.com/dormant/p/5223215.html 1.虚函数(virtual) (1)在某基类中声明为 virtual ...

  3. 性能测试之Jmeter插件安装

    使用Jmeter的实际过程中,需要使用到很多插件,比如json的插件,还有就是做websocket接口测试的时候需要下载websocket的插件,虽然官方提供了插件下载的地址,但是知道为什么每次访问的 ...

  4. js动画---一个小bug的处理

    对于前面的课程,大家似乎看不出来存在什么问题,一切都很顺利,但是其实是存在一个很大的bug的,这个bug是什么呢?? 我们来看看下面这个程序就知道了 <!DOCTYPE html> < ...

  5. 项目Alpha冲刺随笔集合

    班级:软件工程1916|W 作业:项目Alpha冲刺 团队名称:SkyReach 目标:完成项目Alpha版本 项目Github地址 评审表 团队博客汇总 队员学号 队员姓名 个人博客地址 备注 22 ...

  6. 项目Beta冲刺(团队7/7)

    项目Beta冲刺(团队) --7/7 作业要求: 项目Beta冲刺(团队) 1.团队信息 团队名 :男上加男 成员信息 : 队员学号 队员姓名 个人博客地址 备注 221600427 Alicesft ...

  7. http编程体系结构URL loading system

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadi ...

  8. 集合排序、map、枚举

    private void sortList(List<AssessmentQuestionnaireRecord> list){ Collections.sort(list, new Co ...

  9. memoryDiary

    What did you accomplish today? , did you exercise today? Do you care about the people around you tod ...

  10. GitHub上如何创建文件夹

    看了网上很多关于如何在git上创建空文件夹的文章后,发现大家写的都是用指令在本地创建一个空文件夹后再上传指令和步骤都太繁琐且复杂了,对于用git不是很熟练得到人来说太麻烦了,而且在本地于github上 ...