MySQL 主从配置

2018-12-28  本文已影响0人  fangfc

1. MySQL 主从配置

2. 主从配置

  1. 准备主从两个MySQL
[root@node10009 mysql]# ss -tanp |grep 330
LISTEN     0      128    127.0.0.1:3307                     *:*                   users:(("mysqld",pid=3646,fd=34))
LISTEN     0      128         :::3306                    :::*                   users:(("mysqld",pid=965,fd=35))
[root@node10009 mysql]# 
  1. 备份并恢复
[root@node10009 mysql]# mysqldump -uroot -p -P3306 test > /opt/back/test.sql
[root@node10009 mysql]# mysql -uroot -p -P3307 < /opt/backup/test.sql
Enter password: 
[root@node10009 mysql]# 
  1. master 配置文件
[mysqld]
server_id                              = 101
log_bin                                = /opt/app/mysql57/data/binlog/mysql_bin
[root@node10009 mysql]# systemctl restart mysqld
[root@node10009 mysql]# 
MySQL [(none)] > show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000096 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

MySQL [(none)] > GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.10.%' IDENTIFIED BY 'backup';
  1. slave 配置
[mysqld]
server_id                              = 102
relay_log                              = /opt/app/mysql/data/relaylog/relay_log
[root@node10009 mysql]# /opt/app/mysql/bin/mysqladmin -uroot -p -P3306 shutdown
[root@node10009 mysql]# /opt/app/mysql/bin/mysqld_safe --defaults-file=/opt/app/mysql/etc/my.cnf -umysql &
MySQL [test] > CHANGE MASTER TO MASTER_HOST='192.168.10.9',MASTER_USER='slave',MASTER_PASSWORD='123',MASTER_PORT=3306,MASTER_LOG_FILE='mysql_bin.000096',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
MySQL [test] > show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.10.9
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000096
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay_log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql_bin.000096
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
....
  1. 测试
  2. 查看主从数据
001.png
  1. master 修改数据并查看
002.png

END

上一篇下一篇

猜你喜欢

热点阅读