1、说明
1)数据库版本要高于5.1
2)从数据库的版本要 >= 主数据库服务器的版本
4)主数据库IP:192.168.0.235(Windows)
从数据库IP:192.168.0.204(Linux)
2、数据手工同步
目的:自动同步前,保证两个数据库的数据相同。
1)主数据(192.168.0.235)库导出要备份的数据
2)从数据库(192.168.1.103)导入主数据库的备份数据
3、修改主数据库配置文件
1. 找到配置文件(主机IP:192.168.0.235)
d:\MySQL\Mysql-Server-5.7\my.ini
安装版的配置文件默认在数据文件夹中,名称为my.ini(注意:不是在程序文件夹中,程序文件夹中的配置文件名为my-default.ini,修改后不起作用)
2. 修改配置文件,(最好备份一下原来的配置文件)
[mysqld]
#表名存储在磁盘是小写的,但是比较的时候是不区分大小写
lower_case_table_names=1
###主从同步-主###
#设置成IP的最后一位即可
server_id=235
#二进制日志文件名称
log_bin=mysql-bin
#mysql复制模式,三种:SBR(基于sql语句复制),RBR(基于行的复制),MBR(混合模式复制,可靠性更高)
binlog_format=MIXED
#二进制日志自动删除的天数
expire_logs_days=7
# 需要同步的数据库,如果复制多个数据库,重复设置这个选项即可
binlog-do-db=demo
# 不需要同步的数据库
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
binlog-ignore-db = sys
4、修改从数据库配置文件
找到从数据库的配置文件(主机IP:192.168.0.204)/etc/my.cnf
修改配置文件
[mysqld]
#表名存储在磁盘是小写的,但是比较的时候是不区分大小写
lower_case_table_names=1
###主从同步-从###
#设置成IP的最后一位即可
server_id=204
#二进制日志文件名称
log_bin=mysql-bin
#mysql复制模式,三种:SBR(基于sql语句复制),RBR(基于行的复制),MBR(混合模式复制,可靠性更高)
binlog_format=MIXED
#从库设置成只读
read-only=on
super_read_only=on
# 二进制日志自动删除的天数
expire_logs_days=7
#主从复制中的跳过错误处理参数
slave-skip-errors=1062,1053
5、主数据库添加备份用户
连接上主MySql,运行一下命令:
grant replication slave on *.* to 'mycopy'@'%' identified by 'mycopy123';
flush privileges;
#显示主服务器的状态信息,并且找到File 和 Position 的值记录下来;
show master status;
只赋予这个用户 “主从复制” 权限;这个用户用于从服务器同步数据
6 、同步数据
1)锁住主库表flush tables with read lock;
2)备份主库中的所有数据到从库中,保持当前主库和从库数据一致
3)解锁主库表unlock tables;
7、从数据库开启同步
连接从数据库,并输入以下内容:
#重启从数据库,设置登录主数据库的账号和密码等信息,然后启动slave
change master to master_host='${主库的实际IP地址}',master_user='mycopy',master_password='mycopy123', master_log_file='mysql-bin.XXXXXX',master_log_pos=${具体位置};
start slave;
show slave status;
说明:master_host:主服务器的IP地址
master_user:主服务器设置的备份用户
master_password:主服务器备份用户密码
master_log_file:主服务器的日志名称
master_log_pos:主服务器的日志位置
7、验证主从同步成功与否
1)查询从服务器
在从服务器上输入命令:
show slave status;
如果输出
Slave_IO_Running
和Slave_SQL_Running
都是Yes,就说明配置成功了如果其中一项不为
Yes
,查看Last_IO_Errno
错误码和错误信息
,或者查看MySQL
日志信息并查找对应问题
2)添加一个表和数据后,验证一下是否同步到从服务器。
1)从服务器检查SHOW PROCESSLIST;
语句的输出,直到你看到Has read all relaylogwaiting for the slave I/O thread to update it
2)确保从服务器已经处理了日志中的所有语句。STOP SLAVE IO_THREAD;
当从服务器都执行下面指令
STOP SLAVE;
STOP SLAVE io_thread;
RESET SLAVE all;
RESET MASTER;
RESET MASTER说明: 删除所有的binglog日志文件,并将日志索引文件清空,重新开始所有新的日志文件。用于第一次进行搭建主从库时,进行主库binlog初始化工作;但是如果当前是主数据库,且主从数据库正常的时候,千万不能用这种方式删除。
注意reset master 不同于purge binary log的两处地方
1: reset master 将删除日志索引文件中记录的所有binlog文件,创建一个新的日志文件 起始值从000001 开始,然而purge binary log 命令并不会修改记录binlog的顺序的数值
2: reset master 不能用于有任何slave 正在运行的主从关系的主库。因为在slave 运行时刻 reset master 命令不被支持,reset master 将master 的binlog从000001 开始记录,slave 记录的master log 则是reset master 时主库的最新的binlog,从库会报错无法找的指定的binlog文件。RESET SLAVE说明:用于删除SLAVE数据库的relaylog日志文件,并重新启用新的relaylog文件;
reset slave 将使slave 忘记主从复制关系的位置信息。该语句将被用于干净的启动, 它删除master.info文件和relay-log.info 文件以及所有的relay log 文件并重新启用一个新的relaylog文件。
使用reset slave之前必须使用stop slave 命令将复制进程停止。
my.cnf
里read-only=on
改成 read-only=off
4)然后重启mysql服务。
5)在备用服务器(新的主服务器)创建用户
grant replication slave on *.* to 'mycopy'@'%' identified by 'mycopy123';
flush privileges;
RESET MASTER;
。然后change master to MASTER_HOST='原从服务器IP',MASTER_USER='用户名',MASTER_PASSWORD='密码'
,master_log_file='master-bin.000015';
7)查看状态
show slave status;
Show master status;
如果还不行再次重启主库,并在从库上执行
change master to master_host='192.168.0.235',master_user='mycopy',master_password='mycopy123', master_log_file='mysql-bin.000001',master_log_pos=${具体位置};
start slave;
show slave status;
8)修改应用的连接地址到新的主库
切换完成。
1> 在备机上执行STOP SLAVE; 和 RESET MASTER;
2> 查看show slave status;
3> 然后修改应用的连接地址。
一般大部分切换为直接宕机主机已经没法提供服务
生产环境切换记录:
主库和新主库都开启screen 窗口进行操作,避免网络中断引起操作异常
screen -S changemaster
准备工作:停止数据库的写入程序
停止监控
1.锁定老的主库写操作:
master:
flush tables with read lock;
set global read_only=on;
show variables like 'read_only';
show master status;
2.然后修改从数据库为主要数据库:
1> 保证所有从数据库都已经执行了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
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 4757
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;
3> 删除新的主服务器数据库目录中的master.info和relay-log.info文件,否则下次重启时还会按照从服务器来启动,
关闭新主库的my.cnf配置
read_only 注释掉
#### 修改/etc/hosts配置,进行
# db
172.16.0.239 inf.nei.prod.mysql.eus
4> 在新的主库中查看binlog日志
mysql> show master status\G
*************************** 1. row ***************************
File: mysql_bin.000001
Position: 154
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=154;
start slave;
# 从库中执行
mysql> change master to master_host='172.16.0.239' ,master_user='rep',master_password='wsdb123',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.30 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
5>从服务器上检测是否复制正常(Slave_IO_Running: Yes && Slave_SQL_Running: Yes)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.11.0.210
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
解锁定原主数据写操作:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
注意:
1.修改原主库的my.cnf 打开read_only参数,和relay-log相关参数
relay-log = mysql-relay-bin
relay-log-index = relay.index
2.注意新主库的read_only参数是动态修改的,一定要修改my.cnf为off,或者直接删掉,避免重启后为on,应用不可写
进行相应的应用检查
后续工作:
1.数据库备份并且做主从同步
2.从库上做冷备份
添加监控
主库: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
1.问题:unknown variable ‘master-host=192.168.8.1’
解决办法:Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数,需要在mysql控制台中使用change master to 命令添加相关信息,而且中横杠’-‘改成了下横杠’_’…
2.问题:mysqlbinlog: [ERROR] unknown option ‘–no-beep’
解决办法:命令后添加 –no-defualts选项
3.问题:Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决办法:因为copy的时候讲auto.cnf一起copy过来了,就产生了主库和从库UUID重复的问题,删掉auto.cnf,重新启动
4.问题:Got fatal error 1236 from master when reading data from binary log: ‘binlog truncated in the middle of event; consider out of disk space on master; the first event ” at 120, the last event read from ‘D:\mysql\mysql\log\mysql-bin.000001’ at 123, the last byte read from ‘xxx.000001’ at 139.’, Error_code: 1236
解决办法:在从库重新指向到主库下一个可用的binlog file 并且从binlog file初始化的位置开始,执行mysql命令
#此命令一定不要加文件路径,直接写文件名,否则继续报错
mysql> stop slave;
mysql> change master to master_log_file=‘.000001’,master_log_pos=0;
mysql> start slave;
mysql> show slave status;
5.问题:cann’t connect to mysql server 10060,远程无法访问
解决办法:给用户授权远程登录权限
mysql> grant all privileges on . to ‘username’@’%’ identified by ‘password’ with grant option;
6.问题: The innodb_system data file ‘ibdata1’ must be writable
解决办法:赋予ibdata1写入权限