一. 為什么要使用MySQL主從復(fù)制
- 當(dāng)單臺MYSQL服務(wù)器無法滿足當(dāng)前網(wǎng)站流量時的優(yōu)化方案。需要搭建mysql集群技術(shù)。
- 但在實際的生產(chǎn)環(huán)境中,由單臺Mysql作為獨立的數(shù)據(jù)庫是完全不能滿足實際需求的,無論是在安全性,高可用性以及高并發(fā)等各個方面。
- 從安全性角度來說,只有一臺數(shù)據(jù)庫是萬萬不夠的,如果這臺數(shù)據(jù)庫出現(xiàn)故障,將造成不可挽回的損失。這個時候我們需要兩臺甚至多臺數(shù)據(jù)庫來為我們提供數(shù)據(jù)庫服務(wù)。
- 從高可用的角度來說,主從復(fù)試模式具有高性能,可以由多臺slave,實現(xiàn)讀寫分離
- 從高并發(fā)的角度來說,多臺主從服務(wù)器可以實現(xiàn)負(fù)載均衡,把高并發(fā)的請求分發(fā)到不同的服務(wù)器,實現(xiàn)讀寫分離,保證了數(shù)據(jù)的安全。
- 復(fù)試可以實現(xiàn)將數(shù)據(jù)從一臺數(shù)據(jù)庫服務(wù)器(master)復(fù)制到一或多臺數(shù)據(jù)庫服務(wù)器(slave)
- 默認(rèn)情況下屬于異步復(fù)制,無需維持長連接
- 通過配置,可以復(fù)制所有的庫或者幾個庫,甚至庫中的一些表
是MySQL內(nèi)建的,本身自帶的
二. 安裝MySQL服務(wù)器
CentOS 7.0中,已經(jīng)使用MariaDB替代了MySQL數(shù)據(jù)庫,功能和MySQL差不多,這里我們使用MariaDB服務(wù)器進(jìn)行MySQL主從復(fù)制。
安裝所需軟件包:
[root@centos7 ~]#yum -y install httpd mariadb-server mariadb php php-mysql
啟動相關(guān)服務(wù):
[root@centos7 ~]# systemctl start httpd
[root@centos7 ~]# systemctl start mariadb
測試數(shù)據(jù)庫連接:
[root@centos7 ~]# mysql -u root –p123456 –h 10.10.10.68
為root用戶設(shè)置密碼
[root@centos7 ~]# mysqladmin -u root password "123456"
啟動服務(wù)后查看默認(rèn)的數(shù)據(jù)庫:
[root@xuegod64 ~]# systemctl start mariadb
[root@xuegod64 ~]# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
通過密碼登錄:
[root@xuegod64 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
三.主從復(fù)制

環(huán)境準(zhǔn)備.png
[root@xuegod64 html]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database HA;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use HA;
Database changed
MariaDB [HA]> create table T1(
-> id int ,
-> name varchar(20));
Query OK, 0 rows affected (0.03 sec)
MariaDB [HA]> show tables;
+--------------+
| Tables_in_HA |
+--------------+
| T1 |
+--------------+
1 row in set (0.00 sec)
配置my.cnf:
log-bin=mysql-bin-master #啟用二進(jìn)制日志
server-id=1
binlog-do-db=HA #可以被從服務(wù)器復(fù)制的庫, 二進(jìn)制需要同步的數(shù)據(jù)庫名
binlog-ignore-db=mysql
重啟服務(wù)
查看二進(jìn)制日志:
ls /usr/local/mysql/data/
保持同步
復(fù)制前要保證同步的數(shù)據(jù)庫一致
mysqldump -uroot -p123456 HA >HA.sql #可以導(dǎo)出數(shù)據(jù)庫
導(dǎo)出數(shù)據(jù)
將導(dǎo)出的數(shù)據(jù)庫傳給從服務(wù)器
方法:scp HA.sql 10.10.10.64:/root
登錄slave服務(wù)器,執(zhí)行以下命令:
mysql>stop slave; #停止slave
mysql> change master to master_host='10.10.10.63',master_user='slave',master_password='123456';
mysql> start slave; #啟動slave
查看Slave_IO_Running ,Slave_SQL_Running為yes即可。