加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

为什么mysql得使用主从模型

发布时间:2023-10-04 14:48:32 所属栏目:MySql教程 来源:
导读:主从复制的原理:主云服务器(master)上的二进制日志(binlog)中记录的操作,可以在从云服务器(slave)上的中继日志(relaylog)得到重放,进而可以实现数据的同步,保证数据的一致性;

主从复制的前提条件:
主从复制的原理:主云服务器(master)上的二进制日志(binlog)中记录的操作,可以在从云服务器(slave)上的中继日志(relaylog)得到重放,进而可以实现数据的同步,保证数据的一致性;

主从复制的前提条件:
 
1.     主云服务器的数据需要先进行一次完全备份,在从云服务器上恢复;
 
2.     开启免密ssh登录;
 
3.     主云服务器上开启二进制日志,写入server_id,sync_binlog参数设置为1;
 
4.     从云服务器上开启中继日志,写入server_id,开启读锁(从云服务器只可读不可写);
 
从云服务器上对复制相关的线程(show slave status\G);
 
       Slave_IO_Running: Yes
 
              IO_thread:用于和Master相连接,监控和接收Master的二进制日志;
 
Slave_SQL_Running: Yes
 
       SQL_thread:用于监控,读取和重放中继日志的日志信息,并将数据写入到数据库中;
 
根据从云服务器上对复制的线程,我们就可以知道,主从复制就是,master上的操作都被记录在binlog中,然后slave上的IO_thread就将master中的binlog记录的内容复制到本地的relaylog中,最后SQL_thread就将relaylog记录的内容重放,达到数据一致的目标;
 
1.首先需要master和slave可以免密通信;
 
2.对master和slave的主配置文件进行修改;
 
Master的mysql主配置文件:(默认/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
 
 
 
innodb_file_per_table=ON
 
skip_name_resolve=ON
 
log_bin=/var/lib/mysql/binlog   #开启二进制日志
 
server_id=101                   #赋予一个id
 
sync_binlog=1                   #二进制日志同步至磁盘上
 
innodb_flush_log_at_trx_commit=1  #每执行完一个事务后,及时写入磁盘
 
 
 
[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
 
Slave的mysql的主配置文件(默认/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
 
innodb_file_per_table=ON
 
skip_name_resolve=ON
 
server_id=201    #赋予一个server_id
 
read_only=ON   #开启读锁;
 
relay_log=slavelog        #开启slave日志
 
 
 
[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
 
3.启动master,将所有的数据库进行完全备份,并在slave上进行重放;
 
[root@master ~]# mysqldump -uroot -hlocalhost --all-databases -p >  alldata.sql
 
Enter password:
 
[root@master ~]# scp alldata.sql 172.16.75.2:/
 
alldata.sql                                                                100% 2803KB  22.1MB/s   00:00
 
[root@slave ~]# mysql -uroot -p < /alldata.sql
 
Enter password:
 
5.master授权一个具有replication的用户,可以让slave用于登录master进行复制日志内容,并记录当前二进制日志文件名及坐标(show master logs);
 
MariaDB [(none)]> grant replication slave on *.* to 'repuser'@'%' identified by 'reppass';
 
MariaDB [(none)]> show master logs;
 
+---------------+-----------+
 
| Log_name      | File_size |
 
+---------------+-----------+
 
| binlog.000001 |     30379 |
 
| binlog.000002 |   1038814 |
 
| binlog.000003 |      9598 |
 
| binlog.000004 |       647 |
 
| binlog.000005 |       285 |
 
| binlog.000006 |       720 |
 
| binlog.000007 |       264 |
 
| binlog.000008 |       264 |
 
| binlog.000009 |       264 |
 
| binlog.000010 |       264 |
 
| binlog.000011 |  12140997 |
 
+---------------+-----------+
 
11 rows in set (0.00 sec)
 
6.在slave上使用授权用户进行指定master的相关属性信息,并启动复制线程;
 
MariaDB [(none)]> change master to master_host='172.16.75.1',master_user='repuser',master_password='reppass',master_port=3306,master_log_file='binlog.000011',master_log_pos=12140997;
 
 
 
MariaDB [(none)]> start slave;
 
MariaDB [(none)]> show slave status\G
 
*************************** 1. row ***************************
 
               Slave_IO_State: Waiting for master to send event
 
                  Master_Host: 172.16.75.1
 
                  Master_User: repuser2
 
                  Master_Port: 3306
 
                Connect_Retry: 60
 
              Master_Log_File: binlog.000011
 
          Read_Master_Log_Pos: 12140997
 
               Relay_Log_File: slavelog.000018
 
                Relay_Log_Pos: 12141278
 
        Relay_Master_Log_File: binlog.000011
 
             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: 12140997
 
              Relay_Log_Space: 12141846
 
              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: 101
 
1 row in set (0.00 sec)
 
至此,主从模型搭建完毕,当我们在master上进行数据操作时,slave上也会进行记录,并重放至本地数据中;
 
验证:master上创建一个数据表,slave上也查看;
 
master端:
 
MariaDB [hellodb]> create table stu_info(SID int auto_increment  not null primary key);
 
Query OK, 0 rows affected (0.10 sec)
 
MariaDB [hellodb]> desc stu_info;
 
+-------+---------+------+-----+---------+----------------+
 
| Field | Type    | Null | Key | Default | Extra          |
 
+-------+---------+------+-----+---------+----------------+
 
| SID   | int(11) | NO   | PRI | NULL    | auto_increment |
 
+-------+---------+------+-----+---------+----------------+
 
1 row in set (0.00 sec)
 
slave端:
 
MariaDB [(none)]> show tables from hellodb;
 
+-------------------+
 
| Tables_in_hellodb |
 
+-------------------+
 
| classes           |
 
| coc               |
 
| courses           |
 
| scores            |
 
| students          |
 
| teachers          |
 
| toc               |
 
+-------------------+
 
7 rows in set (0.00 sec)
 
MariaDB [(none)]> desc hellodb.stu_info;
 
+-------+---------+------+-----+---------+----------------+
 
| Field | Type    | Null | Key | Default | Extra          |
 
+-------+---------+------+-----+---------+----------------+
 
| SID   | int(11) | NO   | PRI | NULL    | auto_increment |
 
+-------+---------+------+-----+---------+----------------+
 
1 row in set (0.03 sec)
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章