实现mysql主从复制(一主一从)

主从复制配置步骤

1.确保从数据库与主数据库里面的数据一样
2.在主数据库中创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)
5.测试验证
实验需求:

搭建两台mysql服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

环境说明:
数据库角色ip有无数据
主数据库192.168.73.60有数据
从数据库192.168.73.50无数据

分别在主从两台服务器上安装mysql-5.7版本,此处省略安装步骤,如有疑问请参考《二进制格式安装mysql》

1.确保从数据库与主数据库里面的数据一样

为确保从数据库与主数据库里的数据一样,先全备份主数据库并还原到从数据库中
注意:关闭防火墙
1.1模拟主数据库上的数据

mysql> create database  han;
Query OK, 1 row affected (0.01 sec)
mysql> create database dream;
Query OK, 1 row affected (0.00 sec)

mysql> use dream;
Database changed
mysql> insert into tom (id,name,age) values (1,'xiaoming',20),(2,'lihan',18),(3,'zhangsan',3),(4,'lzh',20),(5,'lnr',13);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

先查看主数据库有哪些库

[root@lzh ~]# mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| han                |
| mysql              |
| performance_schema |
| dream            |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

再查看从数据库有哪些库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

1.2全备主库
全备主库时需要另开一个终端,给数据库加上读锁,避免备份期间有其他人在写入导致数据不一致

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.15 sec)
//此锁表的终端必须在备份完成以后才能退出

备份主库,并将备份文件传送到从库

[root@lzh local]# mysqldump  -uroot -p1234.com --all-databases > /root/all-20200508.sql
[root@lzh local]# cd
[root@lzh ~]# ls
all-20200508.sql  anaconda-ks.cfg  
[root@lzh ~]# scp all-20200508.sql root@192.168.73.50:/root
The authenticity of host '192.168.73.50 (192.168.73.50)' can't be established.
ECDSA key fingerprint is SHA256:sHodFwijU+GQJ6N3zjFfs/mweEHWoTrqEivFM2n5HEQ.
ECDSA key fingerprint is MD5:31:e7:89:b4:67:ad:a5:5e:9a:7a:ae:c7:72:ff:c7:36.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.73.50' (ECDSA) to the list of known hosts.
root@192.168.73.50's password: 
all-20200508.sql 

解除主库的锁表状态

mysql> quit
Bye

在从库上导入主库的备份并查看,确保与主库一致

[root@localhost ~]# mysql -uroot -p1234.com < /root/all-20200508.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p1234.com -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dream              |
| han                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

2.在主库上里创建一个同步账号授权给从库使用

mysql> create user 'lzh'@'192.168.73.50' identified by '1234.com';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'lzh'@'192.168.73.50';
Query OK, 0 rows affected (0.00 sec)

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

3.配置主数据库

[root@lzh ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql-bin    //启用binlog日志
server-id = 1         //数据库服务唯一标识符,主库的server-id值必须大于从库
symbolic-links = 0
log-error = /var/log/mysqld.log

重启MySQL服务

[root@lzh ~]# service mysqld restart
Shutting down MySQL... SUCCESS! 
Starting MySQL..... SUCCESS! 
[root@lzh ~]# ss -antl
State       Recv-Q Send-Q  Local Address:Port                 Peer Address:Port              
LISTEN      0      128                 *:22                              *:*                  
LISTEN      0      100         127.0.0.1:25                              *:*                  
LISTEN      0      128                :::22                             :::*                  
LISTEN      0      100               ::1:25                             :::*                  
LISTEN      0      80                 :::3306                           :::* 

查看主库的状态
主库的二进制文件为 mysql-bin.000001
pos值为154

[root@lzh ~]# mysql -uroot -p1234.com
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)

4.配置从数据库

[root@client ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 2                  //设置从库的唯一标识符,从库的server-id必须值小于主库
relay-log = mysql-relay-bin   //启用中继日志relay-log
symbolic-links = 0
log-error = /var/log/mysqld.log

重启从库的mysql服务

[root@client ~]# service  mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@client ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN      0      128                *:111                            *:*                  
LISTEN      0      128                *:22                             *:*                  
LISTEN      0      100        127.0.0.1:25                             *:*                  
LISTEN      0      128               :::111                           :::*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      100              ::1:25                            :::*                  
LISTEN      0      80                :::3306                          :::*   

配置并启动主从复制

mysql> CHANGE MASTER TO 
    ->  MASTER_HOST='192.168.73.60',
    ->  MASTER_USER='lzh',
    ->  MASTER_PASSWORD='123.com',
    ->  MASTER_LOG_FILE='mysql-bin.000001',  //主库的二进制文件
    -> MASTER_LOG_POS=154;                  //主库pos值
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看从服务器状态

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.60
                  Master_User: lzh
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes        **//此处必须为YES**
            Slave_SQL_Running: Yes       **//此处必须为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: 154
              Relay_Log_Space: 527
              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
                  Master_UUID: c769a978-90d4-11ea-908e-000c291a328e
             Master_Info_File: /opt/data/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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

5.测试验证

**在主服务器的dream库的tom表中插入数据**
mysql> use dream;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dream              |
| han                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

Database changed
mysql> select * from tom;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | xiaoming |   20 |
|    2 | lihua    |   18 |
|    3 | zhangsan |    3 |
|    4 | lzh      |   20 |
|    5 | lnr      |   13 |
+------+----------+------+
5 rows in set (0.00 sec)

mysql>  insert into tom values (6,'sean',20),(7,'ld',23);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from tom;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | xiaoming |   20 |
|    2 | lihua    |   18 |
|    3 | zhangsan |    3 |
|    4 | lzh      |   20 |
|    5 | lnr      |   13 |
|    6 | sean     |   20 |
|    7 | ld       |   23 |
+------+----------+------+
7 rows in set (0.00 sec)

在从数据库中查看数据是否同步

mysql> use dream;
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 tom;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | xiaoming |   20 |
|    2 | lihua    |   18 |
|    3 | zhangsan |    3 |
|    4 | lzh      |   20 |
|    5 | lnr      |   13 |
|    6 | sean     |   20 |
|    7 | ld       |   23 |
+------+----------+------+
7 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值