mysql 主从复制以及binlog 测试
Posted feiyun8616的作坊 (半个程序员and dba)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 主从复制以及binlog 测试相关的知识,希望对你有一定的参考价值。
###mysql查看binlog日志内容
https://blog.csdn.net/nuli888/article/details/52106910
mysql的binlog日志位置可通过show variables like \'%datadir%\';查看,直接打开无法查看,要看其内容2个办法:
1、登录到mysql查看binlog
只查看第一个binlog文件的内容
mysql> show binlog events;
查看指定binlog文件的内容
mysql> show binlog events in \'mysql-bin.000002\';
- mysql> show binlog events in \'mysql-bin.000001\';
- +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+
- | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
- +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+
- | mysql-bin.000001 | 4 | Format_desc | 195 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 |
- | mysql-bin.000001 | 106 | Query | 195 | 198 | use `hadoop`; delete from user where id=3 |
- | mysql-bin.000001 | 198 | Intvar | 195 | 226 | INSERT_ID=4 |
- | mysql-bin.000001 | 226 | Query | 195 | 332 | use `hadoop`; INSERT INTO user (id,name)VALUES (NULL,1) |
- | mysql-bin.000001 | 332 | Query | 195 | 424 | use `hadoop`; delete from user where id=3 |
- | mysql-bin.000001 | 424 | Intvar | 195 | 452 | INSERT_ID=5 |
- | mysql-bin.000001 | 452 | Query | 195 | 560 | use `hadoop`; INSERT INTO user (id,name)VALUES (NULL,222) |
- | mysql-bin.000001 | 560 | Query | 195 | 660 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'1\') |
- | mysql-bin.000001 | 660 | Intvar | 195 | 688 | INSERT_ID=6 |
- | mysql-bin.000001 | 688 | Query | 195 | 795 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'555\') |
- | mysql-bin.000001 | 795 | Intvar | 195 | 823 | INSERT_ID=7 |
- | mysql-bin.000001 | 823 | Query | 195 | 930 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'555\') |
- | mysql-bin.000001 | 930 | Intvar | 195 | 958 | INSERT_ID=8 |
- | mysql-bin.000001 | 958 | Query | 195 | 1065 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'555\') |
- | mysql-bin.000001 | 1065 | Intvar | 195 | 1093 | INSERT_ID=9 |
- | mysql-bin.000001 | 1093 | Query | 195 | 1200 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'555\') |
- | mysql-bin.000001 | 1200 | Query | 195 | 1300 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'9\') |
- | mysql-bin.000001 | 1300 | Query | 195 | 1400 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'8\') |
- | mysql-bin.000001 | 1400 | Query | 195 | 1500 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'7\') |
- | mysql-bin.000001 | 1500 | Query | 195 | 1600 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'4\') |
- | mysql-bin.000001 | 1600 | Query | 195 | 1700 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'5\') |
- | mysql-bin.000001 | 1700 | Query | 195 | 1800 | use `hadoop`; DELETE FROM `user` WHERE (`id`=\'6\') |
- | mysql-bin.000001 | 1800 | Intvar | 195 | 1828 | INSERT_ID=10 |
- | mysql-bin.000001 | 1828 | Query | 195 | 1935 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'555\') |
- | mysql-bin.000001 | 1935 | Intvar | 195 | 1963 | INSERT_ID=11 |
- | mysql-bin.000001 | 1963 | Query | 195 | 2070 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'666\') |
- | mysql-bin.000001 | 2070 | Intvar | 195 | 2098 | INSERT_ID=12 |
- | mysql-bin.000001 | 2098 | Query | 195 | 2205 | use `hadoop`; INSERT INTO `user` (`name`) VALUES (\'777\') |
- +------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+
查看当前正在写入的binlog文件
mysql> show master status\\G
- mysql> show master status\\G
- *************************** 1. row ***************************
- File: mysql-bin.000002
- Position: 106
- Binlog_Do_DB:
- Binlog_Ignore_DB: mysql,information_schema,performance_schema
- 1 row in set (0.00 sec)
获取binlog文件列表
mysql> show binary logs;
- mysql> show binary logs;
- +------------------+-----------+
- | Log_name | File_size |
- +------------------+-----------+
- | mysql-bin.000001 | 3548 |
- | mysql-bin.000002 | 106 |
- +------------------+-----------+
- 2 rows in set (0.00 sec)
2、用mysqlbinlog工具查看
基于开始/结束时间
[root@hd3 ~]# mysqlbinlog --start-datetime=\'2016-08-02 00:00:00\' --stop-datetime=\'2016-08-03 23:01:01\' -d hadoop /var/lib/mysql/mysql-bin.000001
基于pos值,注:hadoop是库名,/var/lib/mysql/mysql-bin.000001是二进制文件路径
[root@hd3 ~]# mysqlbinlog --start-position=2098 --stop-position=2205 -d hadoop /var/lib/mysql/mysql-bin.000001
- /*!40019 SET @@session.max_insert_delayed_threads=0*/;
- /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
- DELIMITER /*!*/;
- # at 4
- #160803 17:49:51 server id 195 end_log_pos 106 Start: binlog v 4, server v 5.1.73-log created 160803 17:49:51 at startup
- # Warning: this binlog is either in use or was not closed properly.
- ROLLBACK/*!*/;
- BINLOG \'
- P76hVw/DAAAAZgAAAGoAAAABAAQANS4xLjczLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAAA/vqFXEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
- \'/*!*/;
- # at 2098
- #160803 18:53:56 server id 195 end_log_pos 2205 Query thread_id=1481 exec_time=115 error_code=0
- use `hadoop`/*!*/;
- SET TIMESTAMP=1470221636/*!*/;
- SET @@session.pseudo_thread_id=1481/*!*/;
- SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
- SET @@session.sql_mode=0/*!*/;
- SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
- /*!\\C utf8 *//*!*/;
- SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
- SET @@session.lc_time_names=0/*!*/;
- SET @@session.collation_database=DEFAULT/*!*/;
- INSERT INTO `user` (`name`) VALUES (\'777\')
- /*!*/;
- DELIMITER ;
- # End of log file
- ROLLBACK /* added by mysqlbinlog */;
- /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
#############12
查看是否开启
mysql> show variables like \'log_bin%\';
2、5.7及以后的版本
在5.6的版本情况下需要多添加一个参数
server-id=123456 #123456是唯一的值就好
###通过 server-id 来区分 是否主库 还是 备库
server-id只需采用ip地址的整数形式如:
select INET_ATON(\'10.10.227.198\'),server_id=180806598
#############sample 0 使用mysqlbackup + 主从复制 实施主从 搭建 (for 5.6)
--mysqlbackup在187的/下
--准备工具mysqlbackup
cd /dbsoft/lxy/mysql/nbu
scp * root@10.10.220.156:/tmp/dba
scp * root@10.10.227.196:/tmp/dba
cp mysqlbackup /usr/bin/mysqlbackup
su - mysql
mysqlbackup
#--准备login-path
#find / -name mysql_config_editor
#/db/mysql/app/mysql/bin/mysql_config_editor
#--edit user mysql bash_profile
#PATH=$PATH:$HOME/bin:/db/mysql/app/mysql/bin
#--check
#mysql_config_editor print --login-path=root
##如果报错如下:mysql: unknown variable \'login-path=root\',可以当前的mysql 执行文件有2个。一个是/usr/bin/mysql,.另一个是/db/mysql/bin下,第一个版本太低,低于5.6的版本,可以忽略
##准备mysql 编译版版本介质,并安装
scp mysql-5.6.25.tar.gz root@10.10.227.196:/tmp/dba
cd $mysql_home
mkdir data app
cp mysql-5.6.25.tar.gz $mysql_home
gunzip *.gz
tar -xvf *.tar
mv mysql-5.6.25 mysql
--感谢老刘先生
--https://blog.csdn.net/weiganliu/article/details/49126421
--DCMAKE_INSTALL_PREFIX= 指向mysql安装目录
-DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录
--DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)【默认/etc,可以不指派】
--若想启用某个引擎的支持:-DWITH_<ENGINE>_STORAGE_ENGINE=1
--如:
---DWITH_INNOBASE_STORAGE_ENGINE=1
---DDEFAULT_COLLATION=utf8_general_ci 设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)
--DMYSQL_UNIX_ADDR= /db/mysql/data/mysqltmp/mysql.sock
cmake -DCMAKE_INSTALL_PREFIX=/db/db1/mysql/app -DMYSQL_DATADIR=/db/db1/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/db/db1/mysql/data/mysqltmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install
chown mysql:mysql /db/db1/mysql/app/mysql
ls /usr/bin/mysql
cp /db/db1/mysql/app/bin/mysql /usr/bin/mysql
1、在MySQL的生产从库进行mysqlbackup的全备
##mysqlbackup --login-path=root --socket=/user/my$port/var/mysql.sock --backup-dir=$mybackdir_full ##--backup-image=$mybackdir_full/mybackup.mbi --compress backup-to-image
mysqlbackup -uroot -p --socket=/db/mysql/data/mysqltmp/mysql.sock --backup-dir=/db/mysql/bak \\
--backup-image=/db/mysql/bak/mybackup.mbi --compress backup-to-image
2、将备份文件拷贝到目标主机
cd /db/mysql/bak
scp -rp * root@10.10.227.196:/db/db1/mysql/bak
3、在目标库进行恢复
##mysqlbackup --defaults-file=$mybackdir_full/backup-my.cnf --datadir=/user/my$port/data ##--backup_image=$mybackdir_full/mybackup.mbi --backup_dir=$mybackdir_full copy-back-and-apply-log --uncompress
export mybackdir_full=/db/db1/mysql/bak
##这个命令貌似换行有问题,只能用notepad 放在一行执行,
mysqlbackup --defaults-file=$mybackdir_full/server-my.cnf --datadir=/db/db1/mysql/data/mydata \\ --backup_image=$mybackdir_full/mybackup.mbi --backup_dir=$mybackdir_full copy-back-and-apply-log \\ --uncompress
4、启动数据库
--cd /user/mysql/base
--nohup ./bin/mysqld_safe --defaults-file=/user/my${port}/my.cnf &
cp /db/db1/mysql/bak/server-my.cnf /db/db1/mysql/app/my.cnf
--检查my.cnf
[mysqld]
basedir = /db/db1/mysql/app
datadir = /db/db1/mysql/data/mydata
log-error = /db/db1/mysql/app/mysql_error.log
pid-file = /db/db1/mysql/app/mysql.pid
#user = mysql
#tmpdir = /tmp
tmpdir=/db/db1/mysql/data/mydata/tmp
socket=/db/db1/mysql/data/mysqltmp/mysql.sock
slow_query_log_file=/db/db1/mysql/data/mydata/mysql-slow.log
slave_load_tmpdir=/db/db1/mysql/data/mydata/tmp
plugin_dir=/db/db1/mysql/app/lib/plugin/
slave_load_tmpdir=/db/db1/mysql/data/mydata/tmp
bind_address=10.10.227.198
character_sets_dir=/db/db1/mysql/app/share/charsets/
general_log_file=/db/db1/mysql/data/mydata/pdb1db02.log
lc_messages_dir=/db/db1/mysql/app/share/
log_bin=/db/db1/mysql/data/mydata/mysql-bin
log_bin_index=/db/db1/mysql/data/mydata/mysql-bin.index
log_error=/db/db1/mysql/data/mydata/mysql-error.log
cd /db/db1/mysql/app/
nohup ./bin/mysqld_safe --defaults-file=/db/db1/mysql/app/my.cnf &
##感谢 散尽浮华
##测试GTID 是否要开了
"show variables like \'%gtid%\';"查看
/db/mysql/app/mysql/my.cnf
gtid_mode=on
log-bin=mysql-bin
log-slave-updates=1
enforce-gtid-consistency=1
show master status;
##调整my.cnf文件中的server_id参数,这条命令可以在主库执行,但是IP 用的是从库VIP
###通过 server-id 来区分 是否主库 还是 备库
server-id只需采用ip地址的整数形式如:
select INET_ATON(\'10.10.227.198\'),server_id=180806598
show variables like \'%server_id%\';
5、为了接续复制需要重新设置gtid_purged,
运行$mybackdir_full/meta/backup_gtid_executed.sql重置gtid_purged
--@/db/db1/mysql/bak/meta/backup_gtid_executed.sql
SET @@GLOBAL.GTID_PURGED=\'52714855-1870-11e8-bce2-005056bd438d:1-3\';
6、slave 操作 ,修改MASTER,MASTER_HOST需要设置为生产从库IP
GRANT REPLICATION SLAVE ON *.* TO \'resync\'@\'%\' IDENTIFIED BY \'Resync$123\';
flush flush privileges;
show grants for \'resync\'@\'%\';
CHANGE MASTER TO MASTER_HOST=\'10.10.220.157\', MASTER_USER=\'resync\',Master_Port=3306, MASTER_PASSWORD=\'Resync$123\', MASTER_AUTO_POSITION=1;
7、RESET SLAVE
必须reset,否则会报错:ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
8、START SLAVE
9、SHOW SLAVE STATUS\\G
如下三项值说明同步正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
从库设置为read-only
show variables like \'read_only\';
调整前状态
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
调整为只读
set global read_only=on;
10.mysql> show master status;检查主库状态
11. 主库/备库建表验证
use tmp
create table tb1 ( id int);
create table tb2 ( id int, age int, name char(20), primary key(id) );
12. 数据库名字验证
show databases;
###for mysql 5.7
##感谢关键步就几步
https://www.cnblogs.com/javabg/p/9951852.html
strings /lib64/libc.so.6 | grep GLIBC
cp /dbsoft/lxy/mysql/nbu/mysqlbackup /usr/bin/mysqlbackup
scp mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz cradmin@56.18.99.206:/tmp/dba
tar -xzvf mysql-advanced-5.7.24-linux-glibc2.12-x86_64.tar.gz
groupadd mysql
useradd -r -g mysql mysql
mkdir /crabank/mysql
mysql主目录处理
在software目录下移动文件到/usr/local/mysql:
mv mysql-advanced-5.7.24-linux-glibc2.12-x86_64 /db/mysql
cd /db/mysql
mv mysql-advanced-5.7.24-linux-glibc2.12-x86_64 base
chown -R mysql:mysql /db/mysql
##cp /db/db1/mysql/app/bin/mysql /usr/bin/mysql
cd /db/mysql
mkdir data
show variables like \'%gtid%\';
edit /db/mysql/data/3306/my.cnf
gtid_mode=on
log-bin=mysql-bin
log-slave-updates=1
enforce-gtid-consistency=1
三、重启
su - mysql
mysqladmin -uusername -p shutdown
/db/mysql/base/bin/mysqld_safe --defaults-file=/db/mysql/data/3306/my.cnf &
##mysqld_safe --defaults-file=/**/**/my.cnf &
mysqlbackup -uroot -p --socket=/db/mysql/data/3306/mysqltmp/mysql.sock --backup-dir=/db/mysql/bak \\
--backup-image=/db/mysql/bak/mybackup.mbi --compress backup-to-image
select INET_ATON(\'10.10.227.198\'),
server_id=180806598
############sample 主从复制准备 5.6 版本之前 老的方式
配置主数据库: my.cnf: server-id = 1 log-bin 重启数据库 登录并查看: [root@Mysql-server ~]# mysql -uroot -p199429 mysql> show variables like ‘log_bin‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ mysql> show variables like ‘server_id‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 1 | +---------------+-------+ 建立主从复制账号: mysql> grant replication slave on *.* to ‘rep‘@‘192.1.1.%‘ identified by ‘199429‘; mysql> select user,host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | | bbs | 192.1.1.% | | keer | 192.1.1.% | | rep | 192.1.1.% | | wordpress | 192.1.1.% | | root | localhost | +-----------+-----------+ 实现对主数据库锁表只读: mysql> flush table with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show variables like ‘%timeout%‘; +----------------------------+----------+ | Variable_name | Value | +----------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_lock_wait_timeout | 50 | | innodb_rollback_on_timeout | OFF | | interactive_timeout | 28800 |##### | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | slave_net_timeout | 3600 | | wait_timeout | 28800 |##### +----------------------------+----------+ 10 rows in set (0.00 sec) 查看主库状态: mysql> show master status; +-------------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------------+----------+--------------+------------------+ | Mysql-server-bin.000001 | 962 | | | +-------------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 新开窗口备份导出数据: [root@Mysql-server ~]# mkdir -p /server/backup/ [root@Mysql-server ~]# mysqldump -uroot -p199429 --events -A -B |gzip >/server/backup/mysql_bak.$(date +%F).sql.gz 在此查看主库状态是否变化: mysql> show master status; +-------------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------------+----------+--------------+------------------+ | Mysql-server-bin.000001 | 962 | | | +-------------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 从数据库: 配置文件my.cnf server-id = 2 ####保证唯一性 重启从数据库 登录从数据库: [root@Mysql-server_02 backup]# mysql -uroot -p199429 -S /data/3306/mysql.sock mysql> show variables like ‘log_bin‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | OFF | +---------------+-------+ 1 row in set (0.00 sec) mysql> show variables like ‘server_id‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 3 | +---------------+-------+ 恢复MySQLdump的数据 [root@Mysql-server_02 backup]# cd /server/backup/ [root@Mysql-server_02 backup]# gzip -d mysql_bak.2017-03-23.sql.gz [root@Mysql-server_02 backup]# mysql -uroot -p‘199429‘ -S /data/3306/mysql.sock <mysql_bak.2017-03-23.sql 登录数据库配置复制参数 [root@Mysql-server_02 backup]# mysql -uroot -p‘199429‘ -S /data/3306/mysql.sock mysql> CHANGE MASTER TO -> MASTER_HOST=‘192.1.1.11‘, -> MASTER_PORT=3306, -> MASTER_USER=‘rep‘, -> MASTER_PASSWORD=‘199429‘, -> MASTER_LOG_FILE=‘Mysql-server-bin.000001‘, -> MASTER_LOG_POS=962; 费登录状态执行方法: [root@Mysql-server_02 backup]# mysql -uroot -p‘199429‘ -S /data/3306/mysql.sock<< EOF CHANGE MASTER TO MASTER_HOST=‘192.1.1.11‘, MASTER_PORT=3306, MASTER_USER=‘rep‘, MASTER_PASSWORD=‘199429‘, MASTER_LOG_FILE=‘Mysql-server-bin.000001‘, MASTER_LOG_POS=962; EOF 实际修改从库中的master.info文件 [root@Mysql-server_02 backup]# cat /data/3306/data/master.info 18 mysql-server-bin.000001 962 192.1.1.11 rep 199429 3306 60 0 ,。。。。。。。。 启动主从复制: mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.1.1.11 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Mysql-server-bin.000001 Read_Master_Log_Pos: 962 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 260 Relay_Master_Log_File: Mysql-server-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 962 Relay_Log_Space: 410 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) 测试主从复制功能(省略)
#############sampe 2 测试通过 5.6 版本之前 老的方式
https://www.linuxidc.com/Linux/2016-08/134669.htm
mysql主从复制
mysql支持单向 双向 链式级联 实时 异步复制,在复制过程中,一台服务器充当主服务器(Master),而一个或多个其他服务器充当从服务器(Slave)
mysql主从复制的应用场景
1、主从服务器互为备份
2、主从服务器读写分离分担网站压力
读写分离
中大型公司:通过程序(php,java)
测试环境:代理软件(mysql-proxy,amoeba)
门户网站:分布式dbproxy(读写分离,hash负载均衡,健康检查)
主从同步实践操作(多实例环境)
1、主库上面设置server-id值并开启binlog参数
[root@CentOS03 ~]# egrep "log-bin|server-id" /data/3306/my.cnf
log-bin = /data/3306/mysql-bin
server-id = 1
检查实际配置效果
[root@centos03 ~]# mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show variables like \'log_bin\';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
2、建立用于同步的账号
mysql> grant replication slave on *.* to rep@\'172.16.80.%\' identified by \'123456\';
说明:replication slave 是mysql同步的必须权限,此处不要授权all
mysql> flush privileges;
查看授权后的结果
mysql> show grants for rep@\'172.16.80.%\';
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@172.16.80.% |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO \'rep\'@\'172.16.80.%\' IDENTIFIED BY PASSWORD \'*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9\' |
+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3、锁表,导出数据库
mysql> flush table with read lock; #该窗口不能断,新开一个窗口做数据库导出操作
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 332 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[root@centos03 ~]# mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B --events --master-data=2 > /opt/rep.sql #导出所有数据库
[root@centos03 ~]# vim /opt/rep.sql
-- CHANGE MASTER TO MASTER_LOG_FILE=\'mysql-bin.000002\', MASTER_LOG_POS=332; #可以看到该语句的记录位置和上面show master status是一样的,注释状态
4、数据库导出后,解锁
mysql> show master status; #再次查看位置点,以验证上面的锁表操作是否有效
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 332 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
5、从库上面
[root@centos03 ~]# egrep "log-bin|server-id" /data/3307/my.cnf
#log-bin = /data/3307/mysql-bin #log-bin无需开启
server-id = 3 #server-id的值不能和主库上面的值一样
[root@centos03 ~]# mysql -uroot -p123456 -S /data/3307/mysql.sock < /opt/rep.sql #导入从主库备份的数据库
[root@centos03 ~]# mysql -uroot -phello123 -S /data/3307/mysql.sock
mysql> change master to \\
-> master_host=\'172.16.80.118\',\\
-> master_user=\'rep\',\\
-> master_password=\'123456\',\\
-> master_log_file=\'mysql-bin.000002\',\\
-> master_log_pos=332;
Query OK, 0 rows affected (0.03 sec)
验证一下
[root@centos03 ~]# cat /data/3307/data/master.info
18
mysql-bin.000002
332
172.16.80.118
rep
123456
3306
60
0
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\\G; #观察Slave_IO和Slave_SQL 这两个线程的状态是否是yes
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.80.118
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 332
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 332
Relay_Log_Space: 403
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
6、登录主库创建数据库,看是否会同步到从库上面
[root@centos03 ~]# mysql -uroot -p123456 -S /data/3306/mysql.sock
mysql> create database martin;
Query OK, 1 row affected (0.01 sec)
观察从库,可以看到已经同步过来
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| martin |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
[root@centos03 ~]# cd /data/3307
[root@centos03 3307]# ls
data my.cnf mysql mysqld.pid mysql_martin3307.err mysql.sock relay-bin.000001 relay-bin.000002 relay-bin.index relay-log.info
[root@centos03 3307]# cat data/master.info
18
mysql-bin.000002
419
172.16.80.118
rep
123456
3306
60
0
[root@centos03 3307]# mysqlbinlog relay-bin.000002
/*!\\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database martin
mysql主从复制原理总结:
1、异步同步方式
2、逻辑同步模式,多种模式,默认是通过sql语句执行
3、主库通过记录bin-log实现对从库的同步,bin-log记录数据库更新的语句
4、主库一个IO线程,从库一个IO线程和一个SQL线程
5、从库关键文件master.info relay-log relay-info
6、如果从库还需要做级联从库,从库需要打开log-bin和log-slave-updates参数
监控mysql主从状态(这里我们简单监控从库上面io和sql线程yes总数为不是2就认为主从出现问题了)
在客户端编写脚本
[root@centos03 tools]# cat /tmp/mysql-replication.sh
#!/bin/bash
/application/mysql/bin/mysql -uroot -p123456 -e \'show slave status\\G\' -S /data/3307/mysql.sock|grep -Ei "Slave_IO_Running|Slave_SQL_Running"|awk \'{print $2}\'|grep -c Yes
修改zabbix-agent配置文件
UnsafeUserParameters=1
UserParameter=mysql.replication,/tmp/mysql-replication.sh
在服务器端添加监控项--触发器--图形
正常情况下的图形如下
此时我们模拟主库挂掉
[root@centos03 tools]# /data/3306/mysql stop