一、軟件環境mysql
操做系統:centos-release-7-4.1708.el7.centos.x86_64sql
數據庫:5.5.56-MariaDB數據庫
二、安裝配置數據庫centos
yum install mariadb*socket
systemctl enable mariadb //開機啓動ide
service start mariadb //啓動數據庫測試
service restart mariadb //重啓數據庫操作系統
service stop mariadb //中止數據庫.net
master數據庫配置:rest
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd # 指定惟一的server ID server-id=1 # 打開二進制日誌 log-bin=mysql-bin [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
slave數據庫配置:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd # server-id必須惟一,不能和master或者slave相同 server-id=2 # 配置中繼日誌 relay_log=mysql-relay-bin # 防止改變數據() read_only=1 # 若是此slave須要做爲其餘mysql的master,則須要把下邊兩行主食打開 # log-bin=mysql-bin # log_slave_updates=1 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid
在master數據庫,建立複製用戶:
grant replication slave on *.* to 'foo' identified by 'foo123';
flush privileges;
啓動master數據庫,查看master狀態:
啓動slave數據庫,並鏈接到master:
change master to master_host = '172.16.125.133', master_user='foo', master_password='foo123', master_log_file='mysql-bin.000005', master_log_pos='3522';
啓動slave數據庫:
start slave
中止slave數據庫:
stop slave
重置slave數據庫:
reset slave
查看slave狀態:
其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須爲YES,才代表狀態正常。
三、測試
登陸master數據庫,建立數據庫、表,insert數據:
create database foo default charset utf8 collate utf8_general_ci; create table foo (id bigint primary key auto_increment comment '主鍵', name varchar(100) default null comment '姓名'); insert into foo (name) values ('章三');
登陸slave數據庫, 查看數據庫, 表, 數據是否同步成功!
參考:http://blog.csdn.net/ydyang1126/article/details/70174334