【主从架构】确保主从的两个数据库一致。
um配置 [mariadb] name=MariaDB baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/ gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 服务器1: MariaDB01 192.168.254.28 服务器2: MariaDB02 192.168.254.29 操作系统: CentOS7.3 数据库版本: MariaDB-10.3.7 主从关系: MariaDB01为主,MariaDB02为从 MariaDB01 1. 修改配置文件 vi /etc/my.cnf.d/server.cnf 在[mysqld]下加入以下 server-id=1 log-bin=mysql-bin 2. 重启mysql服务 service mysqld restart 3.创建主从连接帐号与授权 CREATE USER 'slave'@'%' IDENTIFIED BY 'slave'; GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%'; flush privileges; 4.show master status MariaDB02 1. 修改配置文件 vi /etc/my.cnf.d/server.cnf server-id=2 2. 重启mysql服务 service mysqld restart 3.登录数据库 mysql -uroot -proot4. 建立主从连接 CHANGE MASTER TO MASTER_HOST='192.168.254.28', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1443; start slave;
show slave status/G;#竖列查看 5.验证!!! create database test01 use test01 create table students(id int(10), name char(20), age int(10)) 之后查看从节点是否有test01数据库和students表 6.查看mysql二进制log mysqlbinlog /var/lib/mysql/mysql-bin.000001 #如果出现不同步可以执行以下步骤 stop slave set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; start slave 或者 stop slave; mysql> change master to master_host='192.168.254.28', master_user='user', master_password='pwd', master_port=3306, master_log_file='mysql-bin.000008', master_log_pos=483; start slave
【多主架构】
实战Mariadb10.3(10.3自带galera软件) galera Cluster集群架构 Mariadb galera Cluster安装: 操作系统:Centos7.3版本 集群数量:3个节点 主机信息: 192.168.153.142 node1 selinux=disabled firewalld关闭 192.168.153.143 node2 selinux=disabled firewalld关闭 192.168.153.144 node3 selinux=disabled firewalld关闭 搭建步骤 1.主机之间互相解析:三台节点都要执行 vim /etc/hosts 192.168.153.142 node1 192.168.153.143 node2 192.168.153.144 node3 2.安装软件包 第一种方法:(yum install -y MariaDB-server MariaDB-client galera) 配置yum安装源和配置mariadb galera安装源 yum源配置挂iso 设置mariadb的yum源并安装(所有节点都要) 修改yum源文件 vi /etc/yum.repos.d/mariadb.repo [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3.5/centos74-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 enabled=0 安装galera软件时需要解决它的依赖包:boost-program-options.x86_64 (直接yum源安装即可) 3.mariadb初始化 (三个节点都需要执行) 安装完成后会提示需要对mariadb进行初始化(设置密码) systemctl start mariadb mysql_secure_installation (按提示设置mysql密码) systemctl stop mariadb 4.配置galera 主节点配置文件server.cnf vim /etc/my.cnf.d/server.cnf [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so #galera的库文件的地址 wsrep_cluster_address="gcomm://192.168.153.142,192.168.153.143,192.168.153.144" #各节点的ip wsrep_node_name=node1 #节点主机名 wsrep_node_address=192.168.153.142 #节点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 #避免各个节点的数据不一致,这种情况需要等待全同步复制 将此文件复制到mariadb-2、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。 5.启动集群服务: 启动 MariaDB Galera Cluster 服务: (第一次启动要用初始化:mysqld_safe --wsrep_cluster_address=gcomm://192.168.254.24,192.168.254.27,192.168.254.36 >/dev/null &) [root@node1 ~]# /bin/galera_new_cluster 剩余两节点启动方式为: [root@node1 ~]# systemctl start mariadb 查看集群状态:(集群服务使用了4567和3306端口)) [root@node1 ~]# netstat -tulpn | grep -e 4567 -e 3306 tcp 0 0 0.0.0.0:4567 0.0.0.0: LISTEN 3557/mysqld tcp6 0 0 :::3306 ::: LISTEN 3557/mysqld 6.验证集群状态: 在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) 7.测试集群mariad数据是否同步 MariaDB [(none)]> create database lizk; Query OK, 1 row affected (0.010 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | china | | hello | | hi | | information_schema | | lizk | | mysql | | performance_schema | | test | +--------------------+ 8 rows in set (0.001 sec) 在其他两个节点上可以查看lizk库已经同步。 注意: 如果galera_new_cluster 报错 vim /var/lib/mysql/grastate.dat 把safe_to_bootstrap更改为1 # GALERA saved state version: 2.1 uuid: a393feef-f639-11e8-9b89-4e75f9b8fb0f seqno: -1 safe_to_bootstrap: 1