1.首先初始化两个库
[mysql@localhost ~]$ cat initdb.info
#IP SOFT_DIR INSTANCE_BASE_DIR INSTANCE_NAME_PREFIX INSTANCE_NAME INSTANCE_PORT SERVER_ID ADMIN_USER ADMIN_PASSWD
192.168.80.154 /usr/local/mysql-5.7.23/ /home/mysql/mysqldb db master 45678 4567801 root oracle
192.168.80.154 /usr/local/mysql-5.7.23/ /home/mysql/mysqldb db slave 34567 3456701 root oracle
#192.168.80.154 /usr/local/mysql-5.7.23/ /home/mysql/mysqldb db slave01 33344 3334401 root oracle
[mysql@localhost ~]$ sh init57_v1.3_20180402.sh
2.主库创建用户
create user master@'%' identified by 'oracle';
grant all privileges on *.* to master@’%‘;
3.从库中执行命令
CHANGE MASTER TO MASTER_HOST='192.168.80.154',MASTER_USER='master',MASTER_PASSWORD='oracle',MASTER_PORT=45678,MASTER_AUTO_POSITION=1;
4.从库中执行start slave;
查看是否主从连接完成
show slave status;显示两个yes即为成功
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.154
Master_User: master
Master_Port: 45678
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 596
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 811
Relay_Master_Log_File: master-bin.000002
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: 596
Relay_Log_Space: 1014
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: 4567801
Master_UUID: a2c6c2c4-a7c0-11e8-bd23-000c29da9911
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: a2c6c2c4-a7c0-11e8-bd23-000c29da9911:1-2
Executed_Gtid_Set: a2c6c2c4-a7c0-11e8-bd23-000c29da9911:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
5.主库中创建库添加数据验证从库中是否增加数据
主库中
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> use test;
Database changed
mysql> create table t1(x int not null auto_increment primary key);
Query OK, 0 rows affected (0.05 sec)
mysql> ^C
mysql> ^C
mysql> insert into t1 values();
Query OK, 1 row affected (0.18 sec)
mysql> insert into t1 values();
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values();
Query OK, 1 row affected (0.01 sec)
从库中
mysql> select @@port;
+--------+
| @@port |
+--------+
| 34567 |
+--------+
1 row in set (0.00 sec)
mysql> use test;
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
mysql> select * from t1;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
3 rows in set (0.00 sec)
主从搭建成功