17.1
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
17.1 MySQL主從介紹
MySQL主從又叫做Replication、AB復制。簡單講就是A和B兩臺機器做主從后,在A上寫數(shù)據(jù),另外一臺B也會跟著寫數(shù)據(jù),兩者數(shù)據(jù)實時同步的。
MySQL主從是基于binlog的,主上須開啟binlog才能進行主從。
主從過程大致有3個步驟:
三個線程:
- 主上有一個log dump線程,用來和從的I/O線程傳遞binlog
- 從上有兩個線程,其中I/O線程用來同步主的binlog并生成relaylog,另外一個SQL線程用來把relaylog里面的sql語句落地
MySQL主從原理圖:
應用環(huán)境:備份重要數(shù)據(jù)、分擔主庫數(shù)據(jù)讀取壓力
17.2準備工作
安裝并啟動mysql服務;
17.3配置主服務器
主從配置 - 主上操作
安裝mysql 修改my.cnf,增加server-id=130和log_bin=aminglinux1 修改完配置文件后,啟動或者重啟mysqld服務 把mysql庫備份并恢復成aming庫,作為測試數(shù)據(jù) mysqldump -uroot mysql > /tmp/mysql.sql mysql -uroot -e “create database aming” mysql -uroot aming < /tmp/mysql.sql 創(chuàng)建用作同步數(shù)據(jù)的用戶 grant replication slave on *.* to 'repl'@slave_ip identified by 'password'; flush tables with read lock; show master status;編輯配置文件/etc/my.cnf并重啟服務:
[root@localhost ~]# vim /etc/my.cnf …… server-id=132 //自定義 log_bin=DasonCheng1 //指定log前綴 [root@DasonCheng ~]# /etc/init.d/mysqld restart Shutting down MySQL...... SUCCESS! Starting MySQL.................. SUCCESS!新建一個數(shù)據(jù)庫做準備:
[root@DasonCheng ~]# ls -lt /data/mysql/ //服務重啟之后,會生成兩個指定前綴的文件; …… -rw-rw---- 1 mysql mysql 20 8月 30 15:52 DasonCheng1.index -rw-rw---- 1 mysql mysql 120 8月 30 15:52 DasonCheng1.000001 …… [root@DasonCheng ~]# cd /data/mysql/ [root@DasonCheng mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql //備份一個數(shù)據(jù)庫; [root@DasonCheng mysql]# mysql -uroot -p123456 -e "create database Dasontest" //新建一個數(shù)據(jù)庫; [root@DasonCheng mysql]# mysql -uroot -p123456 Dasontest < /tmp/zrlog.sql //將備份的數(shù)據(jù)恢復到新建的數(shù)據(jù)庫中;創(chuàng)建一個用于同步用戶:
[root@DasonCheng mysql]# mysql -uroot -p123456 Welcome to the MySQL monitor. mysql> grant replication slave on *.* to 'repl'@'192.168.8.130' identified by '123456'; Query OK, 0 rows affected (0.01 sec) #IP為“從”的IPmysql> flush tables with read lock; Query OK, 0 rows affected (0.12 sec) #鎖定數(shù)據(jù)表(目的是暫時使其不能繼續(xù)寫,保持現(xiàn)有狀態(tài)用于同步)mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | DasonCheng1.000001 | 10844 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) #記住file和position(設置主從同步時會使用) mysql> quit Bye [root@DasonCheng mysql]# mysqldump -uroot -p123456 Dason > /tmp/Dason.sql [root@DasonCheng mysql]# mysqldump -uroot -p123456 Dason2 > /tmp/Dason2.sql主從配置 - 從上操作
安裝mysql查看my.cnf,配置server-id=132,要求和主不一樣修改完配置文件后,啟動或者重啟mysqld服務把主上aming庫同步到從上可以先創(chuàng)建aming庫,然后把主上的/tmp/mysql.sql拷貝到從上,然后導入aming庫mysql -urootstop slave;change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,start slave;還要到主上執(zhí)行 unlock tables編輯配置文件并重啟
[root@localhost ~]# vim /etc/my.cnf …… server-id=130 …… [root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL...... SUCCESS! Starting MySQL.............................. SUCCESS!配置完成后將主中備份的數(shù)據(jù)發(fā)送到從中:
[root@localhost ~]# scp 192.168.8.132:/tmp/*.sql /tmp/ The authenticity of host '192.168.8.132 (192.168.8.132)' can't be established. ECDSA key fingerprint is 78:22:19:9e:d5:4a:9d:cb:71:54:d7:c0:9a:13:18:9c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.8.132' (ECDSA) to the list of known hosts. root@192.168.8.132's password: Dason2.sql 100% 1259 1.2KB/s 00:00 Dason.sql 100% 1258 1.2KB/s 00:00 zrlog.sql 100% 9864 9.6KB/s 00:00創(chuàng)建庫:
[root@localhost ~]# mysql -uroot Welcome to the MySQL monitor. mysql> create database Dason; Query OK, 1 row affected (0.03 sec) mysql> create database Dason2; Query OK, 1 row affected (0.00 sec) mysql> create database zrlog; Query OK, 1 row affected (0.00 sec) mysql> quit Bye恢復數(shù)據(jù)庫:
[root@localhost ~]# mysql -uroot Dason < /tmp/Dason.sql [root@localhost ~]# mysql -uroot Dason2 < /tmp/Dason2.sql [root@localhost ~]# mysql -uroot zrlog < /tmp/zrlog.sql實現(xiàn)主從同步:
[root@localhost ~]# mysql -uroot Welcome to the MySQL monitor. mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.06 sec) mysql> change master to master_host='192.168.8.132',master_user='repl',master_password='123456',master_log_file='DasonCheng1.000001',master_log_pos=10844; Query OK, 0 rows affected, 2 warnings (0.46 sec) #IP為主的IP;file、pos分別為主的filename和position。檢測主從是否建立成功: mysql> start slave; Query OK, 0 rows affected (0.22 sec)mysql> show slave status\GSlave_IO_Running: Yes Slave_SQL_Running: Yes Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it [root@DasonCheng mysql]# mysql -uroot -p123456 Welcome to the MySQL monitor. mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)主從配置搭建完成;
17.5 測試主從
參數(shù):
主服務器上binlog-do-db= //僅同步指定的庫binlog-ignore-db= //忽略指定庫從服務器上replicate_do_db=replicate_ignore_db=replicate_do_table=replicate_ignore_table=replicate_wild_do_table= //如aming.%, 支持通配符% replicate_wild_ignore_table=- 注: 進行從服務器的配置時盡量使用參數(shù)“replicate_wild_”,使匹配更精確,提升使用性能。
測試:
主服務器:
mysql> show tables; +---------------------------+ | Tables_in_Dasontest | +---------------------------+ | columns_priv | | db | | event | +---------------------------+ 刪除表: mysql> drop table db; mysql> show tables; +---------------------------+ | Tables_in_Dasontest | +---------------------------+ | columns_priv | | event | +---------------------------+從服務器:
主服務器刪除表之前: mysql> show tables; +---------------------------+ | Tables_in_Dasontest | +---------------------------+ | columns_priv | | db | | event | +---------------------------+ 主服務器刪除表之后: mysql> show tables; +---------------------------+ | Tables_in_Dasontest | +---------------------------+ | columns_priv | | event | +---------------------------+配置完成!
轉(zhuǎn)載于:https://my.oschina.net/u/3651233/blog/1527348
總結(jié)
- 上一篇: Ubiquiti几十款无线产品爆出命令注
- 下一篇: 携手Visa IBM Watson Io