zoukankan      html  css  js  c++  java
  • MariaDB数据库(五)

    1. MariaDB主从架构

    1.1 概述

      主从架构用来预防数据丢失。主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构来搭建。

     

    1.2  面试:主从架构同步的实现原理

           对数据库而言,主从架构可以是一主多从,也可以是一主一从,即主数据库是一个,从数据库可以是多个。主从架构要实现同步,至少需要三个线程。当数据写入主之后,主会将数据同步到的Binary log的二进制文件当中,从上面有两个线程:I/O thread、SQL thread, 从的I/O thread会读取主的二进制文件,主的线程log dump thread线程会将数据返回给从,从会将数据写入relay log当中,其SQL线程会读取relay log,进行重放(数据的同步),主从数据一致。

      在主数据库上,一个dump线程会对应一个从数据库,一主多从将会在主数据库上创建多个线程,会增加主数据库的压力,为减轻压力可以在主从之间加如一个代理,与主之间仅有一个线程,与从之间有多个线程,从而很大程度上减轻主的压力。但同样的问题时代理机的压力也会很大,为了解决这个问题,会更换代理数据库的引擎,让代理数据库仅负责二进制文件的读。数据库常用的引擎有MylSAM、InnoDB、blackhole等,blackhole不记录和读取任何数据,对于一主多从架构来说该引擎可以减轻主的压力。

      对于主从架构的同步,在主数据库写的数据能同步到从,但在从数据库写的数据不会同步到主数据库。

      主数据库的bin log的二进制文件是一条一条串行写入的,载入数据时是有延迟的,从节点进一步会延迟,主从数据同步会延迟1~2小时,同时这种延迟对于保障数据库的数据恢复有一定的好处。

      主从架构的同步是异步同步,性能较好,但数据同步不是特别安全。另一种同步实时同步即同步同步,数据更安全,但效率低。

      主从架构可以有多个从数据库,但仅有一个主数据库,存在主数据库单点故障的风险。有两种方法可以预防、解决这种风险:一种是MHA,master high available,MHA会监控主从的状态,当主数据库宕掉后会根据其配置文件从从数据库选举一个数据库来充当主数据库,MHA的一个配置文件可以监控一个主从架构,因此MHA可以实现监控多个主从架构;一种方法是主主架构,即主从互为主从,主从互相监控对方的二进制文件进行数据同步,但主主架构有一个致命的缺点,因为数据库的延迟会出现数据延迟,从而有可能导致主数据库写入数据时会出现冲突,造成数据的不一致,一般企业在数据库增删改少,读多的情况下使用该架构。

    1.3  主从架构的配置

      使用两台主机192.168.16.4、192.168.16.5,.4位主数据库,.5为从数据库

      注意:主从节点的MariaDB版本号要一致,防火墙要关闭。

     主节点:

      1> 修改配置文件

      打开配置文件 /etc/my.cnf.d/server.cnf,在[mysqld]或[server]下加入

      server-id=1

      log-bin=mysql-bin

    [root@localhost ~]# vim /etc/my.cnf.d/server.cnf
    # this is read by the standalone daemon and embedded servers
    [server]
    server_id=1            
    log-bin=mysql-bin                                #名字任意取
    :wq
    MariaDB [(none)]> grant replication slave on *.* to slave@'%' identified by 'slave';
    Query OK, 0 rows affected (0.001 sec)

      2> 重启MariaDB服务

    [root@localhost ~]# systemctl restart mariadb
    [root@localhost ~]#

      3> 进入数据库创建主从连接账号与授权(创建用户并给予权限)

    [root@localhost ~]# mysql -uroot -p123;
    MariaDB [(none)]> grant replication slave on *.* to slave@'%' identified by 'slave';
    Query OK, 0 rows affected (0.001 sec)
     
    MariaDB [(none)]> show master status;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000003 |342       |              |                  |
    +------------------+----------+--------------+------------------+

    从节点

      1> 修改配置文件

    [root@localhost ~]# vim /etc/my.cnf.d/server.cnf
    # this is read by the standalone daemon and embedded servers
    [server]
    server_id=2                              #id与主节点不能重复

      因为是主从架构,所以从节点不必再写入bin log日志文件

      2>  重启MySQL服务

    [root@localhost ~]# systemctl restart mariadb
    [root@localhost ~]#

      3> 登录数据库,建立连接

    [root@localhost ~]# mysql -uroot -p123;
    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.16.4', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=342;
    
    #查看从节点状态
    MariaDB [(none)]> show slave statusG     #G表示按竖字段显示
    …….
                 Slave_IO_Running: No         #I/O线程未启动
                 Slave_SQL_Running:           #SQL线程未启动
    ……… 

      4> 启动线程

    MariaDB [(none)]> start slave;
    MariaDB [(none)]> show slave statusG    
    …….
                 Slave_IO_Running: Connecting #连接中
                 Slave_SQL_Running: Yes
    ………
           Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
    ……
                  Slave_IO_Running: Yes
                 Slave_SQL_Running: Yes
    

      主从节点配置完成后先备份数据库,再进行连接。

    连接、同步

      主节点创建数据库zxjdb,从节点查看是否同步

    #.4
    MariaDB [(none)]> create database zxjdb;
    Query OK, 1 row affected (0.001 sec)
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | testdb             |
    | zxjdb              |
    +--------------------+
    
    #.5
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | testdb             |
    | zxjdb              |                      #成功同步
    +--------------------+
    5 rows in set (0.001 sec)
    
    #在主节点插入数据,从节点查看
    #.4
    MariaDB [(none)]> use zxjdb;
    Database changed
    MariaDB [zxjdb]> create table students (id int,name varchar(20));
    Query OK, 0 rows affected (0.011 sec)
    MariaDB [zxjdb]> insert into students values (1,'lvzxj');
    Query OK, 1 row affected (0.002 sec)
    
    #.5:
    MariaDB [(none)]> use zxjdb;
    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
    MariaDB [zxjdb]> show tables;
    +-----------------+
    | Tables_in_zxjdb |
    +-----------------+
    | students        |
    +-----------------+
    1 row in set (0.000 sec)
    
    MariaDB [zxjdb]> select * from students;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    1 | lvzxj |
    +------+-------+
    1 row in set (0.000 sec)

    2. MariaDB galera集群(多主) 

      galera集群是多主的集群,一个事务由多个节点同时提交,数据同步没有延迟,数据安全性高,属于实时同步。galera集群多用于关键性业务,因为galera集群为了数据的一致性,采用的是同步的机制,这就使galera牺牲了一部分性能来换取数据一致性。

      galera通过wsrap协议实现数据高可用。wsrep默认端口为4567,通过wsrep实现各个节点高可用机制。galera集群最少需要三个节点,假设两个数据库网络网络传输的过程中由于网络故障(如网线坏了)无法通信,但两个数据库并没有宕掉,wsrep协议有将done掉的节点踢出集群的功能,由于两个节点仅由于网络故障失去联系,并没有宕掉,因此会互相抢占主节点,产生脑裂现象(脑裂大部分原因产生于网络),当至少存在三个数据库时可以构成两两相互连接,仅断开一条网络链路(网线)时可以通过其他的网线获知其他数据库的状态,降低脑裂现象的发生的几率。

    2.1  MariaDB集群架构

      操作之前关掉防火墙,关掉selinux。

      galera 在MariaDB 10 之前galera组件是需要单独下载的,10之后集成到了MariaDB-server当中。

      准备三台主机192.168.16.4、192.168.16.5、192.168.16.6进行试验。

    1>  配置文件

    #.4
    [root@localhost ~]# vim /etc/my.cnf.d/server.cnf
    [galera]
    wsrep_on=ON                                         #启动wsrep协议
    wsrep_provider=/usr/lib64/galera/libgalera_smm.so   #galera的库文件的地址
    wsrep_cluster_address="gcomm://192.168.16.4,192.168.16.5,192.168.16.6"                                      #各节点的ip(所有的)
    wsrep_node_name=node1                               #节点主机名(不能重名)
    wsrep_node_address=192.168.16.4 #节点ip(本地主机) binlog_format=row #二进制日志设置为行模式(较耗资源)
    default_storage_engine=InnoDB #使用的默认引擎 innodb_autoinc_lock_mode=2 #性能最好 wsrep_slave_threads=1 #并行复制线程数 innodb_flush_log_at_trx_commit=0 #0.log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作。 #1:每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认。                       #2:每次事务提交时MySQL都会把log buffer的数据写入log file,但是flush(刷到磁盘)操作并不会同时进行。该模式下,MySQL会每秒执行一次 flush(刷到磁盘)操作 innodb_buffer_pool_size=120M #设置缓存池大小 wsrep_sst_method=rsync #远程同步 wsrep_causal_reads=ON  #避免各个节点的数据不一致,这种情况需要等待全同步复制

      发送给.5、.6后直接修改

    [root@localhost ~]# scp /etc/my.cnf.d/server.cnf 192.168.16.5:/etc/my.cnf.d/server.cnf
    root@192.168.16.5's password:
    server.cnf                                               100% 1319     1.3KB/s   00:00   
    [root@localhost ~]# scp /etc/my.cnf.d/server.cnf 192.168.16.6:/etc/my.cnf.d/server.cnf
    root@192.168.16.6's password:
    server.cnf   
    
    #.5
    [root@localhost ~]# vim /etc/my.cnf.d/server.cnf
    [galera]
    wsrep_on=ON
    wsrep_provider=/usr/lib64/galera/libgalera_smm.so
    wsrep_cluster_address="gcomm://192.168.16.4,192.168.16.5,192.168.16.6"
    wsrep_node_name=node2
    wsrep_node_address=192.168.16.5
    binlog_format=row
    default_storage_engine=InnoDB
    innodb_autoinc_lock_mode=2
    wsrep_slave_threads=1
    innodb_flush_log_at_trx_commit=0
    innodb_buffer_pool_size=120M
    wsrep_sst_method=rsync
    wsrep_causal_reads=ON 
    
    #.6
    [root@localhost ~]# vim /etc/my.cnf.d/server.cnf
    wsrep_on=ON
    wsrep_provider=/usr/lib64/galera/libgalera_smm.so
    wsrep_cluster_address="gcomm://192.168.16.4,192.168.16.5,192.168.16.6"
    wsrep_node_name=node3
    wsrep_node_address=192.168.16.6
    binlog_format=row
    default_storage_engine=InnoDB
    innodb_autoinc_lock_mode=2
    wsrep_slave_threads=1
    innodb_flush_log_at_trx_commit=0
    innodb_buffer_pool_size=120M
    wsrep_sst_method=rsync
    wsrep_causal_reads=ON 

    2> 启动集群服务

      第一次启动要初始化。.4初始化启动

    [root@localhost ~]# mysqld_safe --wsrep_cluster_address=gcomm://192.168.16.4,192.168.16.5,192.168.16.6     #初始化
    [root@localhost ~]# /bin/galera_new_cluster            #启动
    [root@localhost ~]# ss -tnl                            #查看端口
    LISTEN   0   128   *:4567    *:*                       #4567端口已启动
    LISTEN   0   80    :::3306    :::*                     #3306端口已启动  
    
    #启动完成后进入文件
    /var/lib/mysql/grastate.dat,复制uuid文件 [root@localhost ~]# vim /var/lib/mysql/grastate.dat # GALERA saved state version: 2.1 uuid: 6320df3d-7b31-11e9-bf58-13052ddac853 seqno: 0 safe_to_bootstrap: 1

      剩余两节点启动之前进入/var/lib/mysql/grastate.dat将uuid改为与.4端一致

      启动方式:systemctl restart mariadb

    #.5
    [root@localhost ~]# systemctl restart mariadb
    [root@localhost ~]# ss -tnl
    LISTEN  0    128     *:4567                *:*  
    LISTEN  0     80     :::3306                :::*   
    
    #.6
    [root@localhost ~]# systemctl restart mariadb
    [root@localhost ~]# ss -tnl
    LISTEN  0    128     *:4567                *:*  
    LISTEN  0     80     :::3306                :::*

    3> 验证集群状态:

      在node1上执行:

    [root@node1 ~]# mysql -uroot -p                    #进入数据库
    
    #查看是否启用galera插件 #连接mariadb,查看是否启用galera插件 MariaDB [(none)]
    > show status like "wsrep_ready"; +------------------+-------+ | Variable_name | Value | +-------------------+-------+ | wsrep_ready | ON | +-------------------+-------+ 1 row in set (0.004 sec)
    #目前集群机器数 MariaDB [(none)]
    > show status like "wsrep_cluster_size"; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | wsrep_cluster_size | 3 | +----------------------+-------+ 1 row in set (0.001 sec)
    #查看集群状态 MariaDB [(none)]
    > show status like "wsrep%"; +------------------------------+--------------------------------------+ | Variable_name | Value | +------------------------------+--------------------------------------+ | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 1.000000 | | wsrep_causal_reads | 14 | | wsrep_cert_deps_distance | 1.200000 | | wsrep_cert_index_size | 3 | | wsrep_cert_interval | 0.000000 | | wsrep_cluster_conf_id | 22 | | wsrep_cluster_size | 3 | #集群成员 | wsrep_cluster_state_uuid | b8ecf355-233a-11e8-825e-bb38179b0eb4 | #UUID 集群唯一标记 | wsrep_cluster_status | Primary | #主服务器 | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 1.000000 | | wsrep_connected | ON | #当前是否连接中 | wsrep_desync_count | 0 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_recv | 0 | | wsrep_flow_control_sent | 0 | | wsrep_gcomm_uuid | 0eba3aff-2341-11e8-b45a-f277db2349d5 | | wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 | #连接中的数据库 | wsrep_last_committed | 9 | #sql 提交记录 | wsrep_local_bf_aborts | 0 | #从执行事务过程被本地中断 | wsrep_local_cached_downto | 5 | | wsrep_local_cert_failures | 0 | #本地失败事务 | wsrep_local_commits | 4 | #本地执行的sql | wsrep_local_index | 0 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_avg | 0.057143 | | wsrep_local_recv_queue_max | 2 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | #本地发出的队列 | wsrep_local_send_queue_avg | 0.000000 | #队列平均时间间隔 | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_local_state_uuid | b8ecf355-233a-11e8-825e-bb38179b0eb4 | #集群ID | wsrep_protocol_version | 8 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <info@codership.com> | | wsrep_provider_version | 25.3.23(r3789) | | wsrep_ready | ON | #插件是否应用中 | wsrep_received | 35 | #数据复制接收次数 | wsrep_received_bytes | 5050 | | wsrep_repl_data_bytes | 1022 | | wsrep_repl_keys | 14 | | wsrep_repl_keys_bytes | 232 | | wsrep_repl_other_bytes | 0 | | wsrep_replicated | 5 | #随着复制发出的次数 | wsrep_replicated_bytes | 1600 | #数据复制发出的字节数 | wsrep_thread_count | 2 | +------------------------------+--------------------------------------+ 58 rows in set (0.003 sec) #查看连接的主机 MariaDB [(none)]> show status like "wsrep_incoming_addresses"; +--------------------------+----------------------------------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------------------------------+ |wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 | +--------------------------+----------------------------------------------------------------+ 1 row in set (0.002 sec)

    4>  测试集群mariadb数据是否同步

    MariaDB [(none)]> create database zxj;
    Query OK, 1 row affected (0.009 sec)
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | zxj                |
    +--------------------+

      在其他两个节点上可以查看lizk库已经同步.

    #.5:
    [root@localhost ~]# mysql -uroot -p123;
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | zxj                |
    +--------------------+
    
    #.6:
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | zxj                |
    +--------------------+

      再在.5端删除数据库,在.4、.6端查看

    #.5
    MariaDB [(none)]> drop database zxj;
    MariaDB [(none)]> show databases;
    +------------------------+
    | Database               |
    +------------------------+
    | information_schema     |
    | mysql                  |
    | performance_schema     |
    +------------------------+
    
    #.4端:
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    
    #.6端:
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+

    注意:

      如果galera_new_cluster 报错,进入 /var/lib/mysql/grastate.dat

      把safe_to_bootstrap更改为1

    [root@localhost~ ]#vim  /var/lib/mysql/grastate.dat
    # GALERA saved state
    version: 2.1
    uuid:    a393feef-f639-11e8-9b89-4e75f9b8fb0f
    seqno:   -1
    safe_to_bootstrap: 1
  • 相关阅读:
    Sql Server 日期时间格式转换
    Windows7中pagefil.sys和Hiberfil.sys文件删除与转移
    64位机的pl/sql不安装32位oracle的连接方式
    cmd下进入oracle sqlplus
    杂七杂八
    做题记录Ⅱ
    SPOJ GSS8
    AGC036 A-Triangle | 构造
    Atcoder 题目泛做
    CF398A Cards | 贪心
  • 原文地址:https://www.cnblogs.com/ajunyu/p/10921605.html
Copyright © 2011-2022 走看看