1.部署
10.0.0.21 MariaDB-21
10.0.0.22 MariaDB-22
cat /etc/yum.repos.d/mariadb.repo [mariadb] name=MariaDB baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/ gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 yum -y install MariaDB-server MariaDB-client systemctl start mariadb # 除了將密碼改為mariadb123456,其余步驟都按Y /usr/bin/mysql_secure_installation # 如果需要修改默認的數據路徑,先不要啟動mariadb mkdir -p /data/mariadb chown -R mysql:mysql /data/mariadb # 將默認的數據拷貝到新的路徑下面 cp -a /var/lib/mysql/* /data/mariadb/ # 修改配置文件/etc/my.cnf.d/server.cnf [mysqld] datadir=/data/mariadb socket=/var/lib/mysql/mysql.sock character_set_server=utf8 slow_query_log=on slow_query_log_file=/data/mariadb/logs/slow_query_log.log long_query_time=2 log-error =/data/mariadb/logs/error.log # 本實驗一切按照默認的來
2.配置主從復制
a.在10.0.0.21上配置主服務器
innodb_file_per_table:拆分表數據存儲 log-bin:開啟二進制日志 cat /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve = ON innodb_file_per_table = ON server-id = 21 log-bin = master-bin log-bin-index=master-bin.index systemctl restart mariadb.service mysql -uroot -pmariadb123456 grant replication slave,replication client on *.* to 'repluser'@'10.0.0.%' identified by 'repluser123456'; flush privileges; # 查看主服務器的狀態信息,在從服務器中要用到 show master status\G File: master-bin.000001 Position: 676
b.在10.0.0.22上配置從服務器
從服務器最好啟用read_only=ON禁止寫操作,防止誤操作 cat /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve = ON innodb_file_per_table = ON server-id = 22 relay_log = slave_relay_bin systemctl restart mariadb.service mysql -uroot -pmariadb123456 change master to master_host='10.0.0.21',master_user='repluser', master_password='repluser123456',master_log_file='master-bin.000001',master_log_pos=676; start slave; show slave status\G Slave_IO_Running: Yes Slave_SQL_Running: Yes
測試,在主服務器上創建庫,創建表,插入數據,可以在從服務器上看到數據被同步過來
create database TestDB; use TestDB; create table student(id int,name varchar(20)); insert into student(id,name) values('1','zhangsanfeng'); insert into student(id,name) values('2','budaiheshang'),('3','wusanren'); insert into student set id=4,name="zhangwuji";
3.配置主主復制
在上面主從復制的基礎上繼續做,即:在之前的master上開啟relay_log,在之前的slave上開啟log-bin,並且把步進值改為n
auto_increment_offset:起始值,一般填第n台主MySQL,此時為第一台主MySQL
auto_increment_increment:步進值,一般有n台主MySQL就填n
binlog-ignore:忽略某個庫,這里先不用這個參數
replicate-do-db:要同步的數據庫,默認所有庫
10.0.0.21的配置文件內容如下
cat /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve = ON innodb_file_per_table = ON server-id = 21 auto_increment_offset = 1 auto_increment_increment = 2 relay_log = slave_relay_bin log-bin = master-bin log-bin-index = master-bin.index
10.0.0.22的配置文件內容如下
cat /etc/my.cnf.d/server.cnf [mysqld] skip_name_resolve = ON innodb_file_per_table = ON server-id = 22 relay_log = slave_relay_bin auto_increment_offset = 2 auto_increment_increment = 2 log-bin = master-bin log-bin-index = master-bin.index
修改完配置文件之后,兩台服務器都要重啟
systemctl restart mariadb.service # 在10.0.0.22上授權用戶 mysql -uroot -pmariadb123456 grant replication slave,replication client on *.* to 'repluser'@'10.0.0.%' identified by 'repluser123456'; flush privileges; # 查看二進制日志名和位置 show master status\G File: master-bin.000001 Position: 329 # 在10.0.0.21中執行 change master to master_host='10.0.0.22',master_user='repluser', master_password='repluser123456',master_log_file='master-bin.000001',master_log_pos=329; start slave;
完成主主復制配置,在10.0.0.22上進行測試,可在21上看到數據已經被同步
use TestDB; create table tab1(id int auto_increment,name varchar(10),primary key(id)); insert into tab1 (name) value('zhangsan'),('lisi'),('wangmazi');
主主復制配置文件中auto_increment_increment和auto_increment_offset只能保證主鍵不重復,卻不能保證主鍵有序.
主從|主主寫的不錯:https://www.cnblogs.com/phpstudy2015-6/p/6485819.html
主從|主主|半同步都有寫:https://www.jb51.net/article/97786.htm