mysql 主从复制 实践
2018-06-17 22:56:21来源:未知 阅读 ()
- 备份还原
-
- 使用mysqldump或者xtrabackup
- 把主库现有基础数据还原到从库
- 授权
-
- grant replication slave on *.*
- 给从库一个复制binlog的账号
- 配置复制,并启动
-
- 从库上配置复制信息,并指向master
- 查看主从复制信息
-
- show slave status \G
aiapple@ubuntu:~$ mysqldump -uroot -p --socket=/tmp/mysqldata/node1/mysql.sock --master-data --all-databases > all_master.sql --master-date:记录备份时binlog位置 aiapple@ubuntu:~$ cat all_master.sql | less -- -- Position to start replication or point-in-time recovery from -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=6125; -- -- Current Database: `db1` --
mysql> grant all on *.* to root@192.168.1.100 identified by '000000' with grant option;
aiapple@ubuntu:~$ mysql -uroot -p -h 192.168.1.100 mysql> source all_master.sql
mysql> grant replication slave on *.* to repl@192.168.1.100 identified by 'repl'; Query OK, 0 rows affected (0.00 sec)
#查看帮助信息 ? change master to mysql> change master to MASTER_USER='repl'; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> change master to MASTER_PASSWORD='repl'; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> change master to MASTER_HOST='192.168.1.101'; Query OK, 0 rows affected (0.03 sec) mysql> change master to MASTER_LOG_FILE='mysql-bin.000001';
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; slave_io_running:yes slave_sql_running:yes #表示配置成功 mysql> show processlist; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+ | 2 | root | localhost | NULL | Query | 0 | init | show processlist | | 16 | system user | | NULL | Connect | 60 | Waiting for master to send event | NULL | | 17 | system user | | NULL | Connect | 60 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
- 主从server_id应该不同;
- 主从开启binlog日志
- MASTER_LOG_FILE指定主库bin_log第一个文件;
show plugin
1.主从异步复制搭建
1)主库全备,备库恢复 mysqldump -uroot -p123456 --socket=/data/mysql/node1/mysqld.sock --single-transaction -A --master-data=1 > all_db.sql mysql -utest -ptest -h(从库IP) -P3306 mysql>source all_db.sql; 2)主库授权用户 grant replication slave on *.* to repl@'(从库IP)' identified by 'repl'; 3)从库配置复制 less all_db.sql|grep "change master to" change master to master_host='(主库IP)',master_user='repl',master_password='repl',master_log_file='XXX',master_log_pos=XXX; start stave; show slave status\G 4)复制检验 主库: use db1; insert into t1 values(10); 从库: use db1; select * from t1;(获得数据) 主库: drop database db2; 从库: show databases;(显示db2被删除) 5)查看线程 主库:show processlist;(dump线程) 从库:show processlist;(IO线程、SQL线程) 6)查看日志 从库:cd /data/mysql/node1 cat master.info cat relay-log.info
1)主库安装插件 show plugins; install PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; 2)从库安装插件 show plugins; INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; 3)参数设置 主库: show variables like '%semi%'; SET GLOBAL rpl_semi_sync_master_enabled=1; 从库: SET GLOBAL rpl_semi_sync_slave_enabled=1; 4)重启主从复制 从库: stop slave; start slave; 5)状态检查 show global status like '%semi%'; 6)复制检查 主库: use db1; insert into t1 values(100); 从库: use db1; select * from t1;(获得数据) 7)测试延迟 从库: stop slave; 主库: use db1; insert into t1 values(1);(被卡10s) set global rpl_semi_sync_master_timeout=1000;(设置主等从时间1秒) 从库: start slave; stop slave; 主库: use db1; insert into t1 values(88);(被卡1s)
#从库: show variables like '%slave_par%'; set global slave_parallel_workers=10; 设置sql线程数为10 #重启 slave stop slave; start slave; #查看线程 show processlist;(十个worker线程)
1)主库: create database db2; 2)从库部分复制配置 配置文件中增加配置 #vim /data/mysql/my1.cnf(replicate_do_db=db2) 重启mysql #mysqladmin -uroot --socket=XXX --port=3306 -p123456 shutdown #/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/my1.cnf & show slave status; 显示:replicate_do_db=db2 3)测试 主库: use db1; delete from t1; 从库: use db1; select * from t1;(任然保留数据) 主库: use db2; create table user(a int,b int); 从库: use db2; show tables;(查看到user表)
1)从库: #vim /data/mysql/my1.cnf(log_slave_updates) #mysqladmin -uroot --socket=XXX --port=3306 -p123456 shutdown #/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/my1.cnf & 2)创建新实例 在主库服务器上创建一个从库2实例 #mysqladmin -uroot --socket=XXX --port=3306 -p123456 shutdown #kill -9 (mysqld_safe进程号) #cp -r node1 node2 #vim my.cnf(修改相关参数,端口3307) #chown -R mysql.mysql node2 #/usr/local/mysql56/bin/mysqld_safe --defaults-file=/data/mysql/my1.cnf & #/usr/local/mysql56/bin/mysqld_safe --defaults-file=/data/mysql/my2.cnf & #mysqldump -utest -ptest -hXXX -P3306 -A --master-data=1 > d731.sql(dump从库1的全备) #mysql -uroot --socket=/data/mysql/node2/mysqld.sock -p123456 < d731.sql 3)配置从1和从2的主从 从1授权: grant replication slave on *.* to repl@'(从2IP)' identified by 'repl'; 从2配置复制: less d731.sql|grep "change master to" change master to master_host='(从1IP)',master_user='repl',master_password='repl',master_log_file='XXX',master_log_pos=XXX; start stave; show slave status\G show processlist; 4)联级复制测试 主库: create database db3; 从1: show databases;(获得新建库) 从2: show databases;(获得新建库)
#查看状态
show slave status;
- 手动处理
- 跳过复制错误:set global sql_slave_skip_counter=1
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MySQL replace函数怎么替换字符串语句 2020-03-09
- PHP访问MySQL查询超时怎么办 2020-03-09
- mysql登录时闪退 2020-02-27
- MySQL出现1067错误号 2020-02-27
- mysql7.x如何单独安装mysql 2020-02-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash