1,在主机A,B上关闭防火墙和selinux
[root@localhost ~] systemctl stop firewalld
[root@localhost ~] setenforce 0
2,在主机A,B上安装mysql
[root@localhost ~] yum -y install mariadb mariadb-[root@localhost ~] server #centos7上mysql服务名称为:mariadb
3,在主机A上开启二进制日志
[root@localhost ~] vim /etc/my.cnf
[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=1 #定义服务id主为1
log-bin=mysql-bin #开启二进制日志
binlog-ignore-db=information_schema #不同步该库
binlog-ignore-db=performance_schema #不同步该库
binlog-ignore=mysql #不同步mysql库
[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
启动mysql
[root@localhost ~] '#' systemctl start mariadb
4,登录mysql,
[root@localhost ~] mysql #初始用户为root,密码0,可直接登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> grant all on *.* to slave@'192.168.59.111' identified by '123'; #创建用户 %处可以写授权的主机ip,用户名:slave,密码:123
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; #刷新生效
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status; #查看二进制日志
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 462 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
5,在主机B上开启二进制日志
[root@localhost ~] vim /etc/my.cnf
[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=2 #定义服务id主为2
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
启动mysql
[root@localhost ~] systemctl start mariadb
6,登录mysql,
#首先测试在主机B上能否登陆mysql主库
[root@localhost ~] mysql -uslave -p123456 -h192.168.59.110
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
#开始登陆从库
[root@localhost ~] mysql #初始用户为root,密码0,可直接登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> slave stop; #关闭slave
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> change master to master_host='192.168.59.110',master_user='slave',master_password='123',master_log_file='mysql-bin.000003',master_log_pos=462; #设置主从同步
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> slave start; #开启slave
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status \G; # 查看是否同步
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.59.110
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 931
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 462
Relay_Log_Space: 825
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
ERROR: No query specified
出现双yes,则为配置成功,
7,可以再主机A上建库,建表
MariaDB [(none)]> create database student;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use student;
Database changed
MariaDB [student]> create table zmedu(id int,cont varchar(10));
Query OK, 0 rows affected (0.01 sec)
MariaDB [student]> insert into zmedu values(1,44), (2,33);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
在主机B上查看同步
MariaDB [(none)]> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [student]> select * from zmedu;
+------+------+
| id | cont |
+------+------+
| 1 | 44 |
| 2 | 33 |
+------+------+
2 rows in set (0.00 sec)