MySQL 传统复制(MySQL 5.7 -- MySQL 8.0)

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/vkingnew/article/details/89494810
主库:MySQL 5.7.25    10.19.162.134
从库: MySQL 8.0.15   10.19.145.159

1.源库配置:
# vi /etc/my.cnf
...
server-id=100
log-bin=mysql_bin.log
binlog_format=ROW
expire-logs-days=2
...

2.目标库配置:
#vi /etc/my.cnf
...
server-id=120
binlog_format=ROW
log-bin= bin.log
relay-log= relay.log
expire-logs-days=2
relay_log_recovery=1
slave-net-timeout=60
relay_log_purge=1
log_slave_updates=on
binlog_expire_logs_seconds     =86400
...


3.源库和目标库均创建用户和授权:
mysql> create user 'repl'@'%' identified by 'repl';
Query OK, 0 rows affected (0.03 sec)

mysql> grant REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' ;
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

--语句如下:
create user 'repl'@'%' identified by 'repl';
grant REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' ;
flush privileges;


4.获取源库的binlog和pos:
mysql> reset master;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

若新搭建的可以用上述部署。已运行业务的则需要备份还原数据到从库。

5.在从库接续源库:
mysql> 
CHANGE MASTER TO
  MASTER_HOST='10.19.162.134',
  MASTER_USER='repl',
  MASTER_PASSWORD='repl',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql_bin.000001',
  MASTER_LOG_POS=154,
  MASTER_CONNECT_RETRY=10;

启动从库:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G


--查看从库的同步状态和延迟:
Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.19.162.134
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...
Seconds_Behind_Master: 0
...

6.新建库表做测试:
在主库写入:
mysql> create database wuhan;
Query OK, 1 row affected (0.00 sec)

mysql> create table wuhan.user(id bigint unsigned not null primary key,username varchar(20),city_id int,city_name varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into wuhan.user(id,username,city_id,city_name)values(1,'user001',100,'wuhan city');
Query OK, 1 row affected (0.01 sec)

mysql> select * from wuhan.city;
ERROR 1146 (42S02): Table 'wuhan.city' doesn't exist
mysql> select * from wuhan.user;
+----+----------+---------+------------+
| id | username | city_id | city_name  |
+----+----------+---------+------------+
|  1 | user001  |     100 | wuhan city |
+----+----------+---------+------------+
1 row in set (0.00 sec)

在从库查询:
mysql> select * from wuhan.user;
+----+----------+---------+------------+
| id | username | city_id | city_name  |
+----+----------+---------+------------+
|  1 | user001  |     100 | wuhan city |
+----+----------+---------+------------+
1 row in set (0.00 sec)


后面可以做update、delete操作,加减字段,加减表操作,再在从库查看对应的表。


猜你喜欢

转载自blog.csdn.net/vkingnew/article/details/89494810
今日推荐