1、 从MySQL的主从复制和读写分离,到ZooKeeper分布式协调服务,再到Kafka消息中间件和Nginx的代理功能,此功能主要同步各个主机之间的配置文件和一些重要的信息,方便运维人员不用再从每个机器上来更改信息,只需要注册了监听机器IP就能实现同步。
2、Kafka消息中间件主要是为了防止消息锁死,方便用户随用随取。
3、Nginx在本章主要实现了代理作用,其实Nginx的功能很多,譬如负载均衡、web服务、缓存等,现在很多企业都在使用这个Nginx服务。
4、最后,介绍了Zabbix这个监控开源平台,该平台主要方便运维人员及时发现服务器问题和解决问题。
① 数据分布(Data distribution)
② 负载平衡(load balancing)
③ 备份(Backups)
④ 高可用性和容错行(High availability and failover)
① Master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件(binary log events); ② Slave将master的binary log events拷贝到它的中继日志(relay log); ③ Slave重做中继日志中的事件,将改变反映它自己的数据。
复制概述
MySQL内建的复制功能是构建大型、高性能应用程序的基础。
1、将MySQL的数据分布到多个系统上去,这种分布的机制,是通过将MySQL的某一台主机的数据复制到其它主机(slave)上,并重新执行一遍来实现的。
2、复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引来跟踪日志循环。这些日志可以记录发送到从服务器的更新。
3、当一个从服务器连接主服务器时,它通知主服务器和从服务器在日志中读取最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。
规划节点
IP | 主机名 | 节点 |
---|---|---|
10.30.59.205 | db1 | MariaDB数据库集群主节点 |
10.30.59.215 | db2 | MariaDB数据库集群从节点 |
10.30.59.222 | mycat | Mycat中间件服务节点 |
2台虚拟机db1和db2部署MariaDB数据库服务,搭建主从数据库集群;一台作为主节点,负责写入数据库信息;另一台作为从节点,负责读取数据库信息。
使用一台虚拟机部署Mycat数据库中间件服务,将用户提交的读写操作识别分发给相应的数据库节点。这样将用户的访问操作、数据库的读与写操作分给3台主机,只有数据库集群的主节点接收增、删、改SQL语句,从节点接收查询语句,分担了主节点的查询压力。
修改主机名
[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]#
[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#
[root@localhost ~]# hostnamectl set-hostname db2
[root@localhost ~]# bash
[root@db2 ~]#
编辑hosts文件(在所有节点)
[root@db1 ~]# vi /etc/hosts
10.30.59.205 db1
10.30.59.215 db2
10.30.59.222 mycat
secureFX上传文件(所有节点)
[root@mycat ~]# ll
total 326660
-rw-------. 1 root root 1260 Mar 29 23:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 318829093 Jun 18 11:01 gpmall-repo.tar.gz
-rw-r--r--. 1 root root 15662280 Jun 18 10:14 Mycat-server-1.6-RELEASE-20161028204710-linux .tar.gz
[root@db1 ~]# mv gpmall-repo.tar.gz /opt
[root@db1 ~]# cd /opt
[root@db1 ~]# ll
[root@db1 ~]# yum install -y unzip
[root@db1 ~]# unzip gpmall-repo.tar.gz
配置Yum安装源(所有节点)
[root@db1 ~]# cd /etc/yum.repos.d/
[root@db1 yum.repos.d]# ll
total 32
-rw-r--r--. 1 root root 1664 Apr 29 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Apr 29 2018 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Apr 29 2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Apr 29 2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Apr 29 2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Apr 29 2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 Apr 29 2018 CentOS-Vault.repo
[root@db1 yum.repos.d]# mv /etc/yum.repos.d/C* /media/
[root@db1 yum.repos.d]# mkdir /opt/cdrom
[root@db1 yum.repos.d]# mount /dev/cdrom /opt/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@db1 yum.repos.d]# vi /etc/yum.repos.d/local.repo
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
[cdrom] 本地yum源
name=cdrom
baseurl=file:///opt/cdrom
gpgcheck=0
enabled=1
[root@db1 yum.repos.d]# yum clean all
[root@db1 yum.repos.d]# yum repolist
关闭防火墙(所有节点)
[root@db1 ~]# systemctl stop firewalld
[root@db1 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@db1 ~]# setenforce 0
[root@db1 ~]# iptables -F
[root@db1 ~]# iptables -X
[root@db1 ~]# iptables -Z
[root@db1 ~]# iptables-save
配置 ftp 连接另两个节点(在db1节点)
[root@db1 ~]# yum install -y vsftpd
[root@db1 ~]# vim /etc/vsftpd/vsftpd.conf
anon_root=/opt 添加此段
[root@db1 ~]# systemctl start vsftpd
[root@db1 ~]# systemctl enable vsftpd
配置 ftp 连接另两个节点(在db2节点)
[root@db2 yum.repos.d]# curl ftp://10.30.59.205
-rw-r--r-- 1 0 0 15662280 Jun 18 02:14 Mycat-server-1.6-RELEASE-20161028204710-linux .tar.gz
drwxr-xr-x 8 0 0 2048 May 03 2018 cdrom
drwxr-xr-x 5 0 0 53 Nov 21 2020 gpmall-repo
[root@db2 yum.repos.d]# vi ftp.repo
[cdrom]
name=cdrom
baseurl=ftp://10.30.59.205/cdrom
gpgcheck=0
enabled=1
[gpmall]
name=gpmall
baseurl=ftp://10.30.59.205/gpmall-repo
gpgcheck=0
enabled=1
[root@db2 yum.repos.d]# yum clean all
[root@db2 yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
gpmall | 2.9 kB 00:00:00
mariadb | 3.6 kB 00:00:00
(1/3): mariadb/group_gz | 166 kB 00:00:00
(2/3): gpmall/primary_db | 144 kB 00:00:00
(3/3): mariadb/primary_db | 3.1 MB 00:00:00
repo id repo name status
gpmall gpmall 165
mariadb mariadb 3,971
repolist: 4,136
安装JDK环境(在mycat安装)
[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-devel
[root@mycat ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
关闭防火墙(所有节点)
[root@db2 ~]# iptables -F
[root@db2 ~]# iptables -X
[root@db2 ~]# iptables -Z
[root@db2 ~]# iptables-save
# Generated by iptables-save v1.4.21 on Mon Jun 21 18:06:56 2021
*filter
:INPUT ACCEPT [21:1648]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [11:1012]
COMMIT
# Completed on Mon Jun 21 18:06:56 2021
安装MariaDB服务(db1和db2节点)
[root@db1 ~]# yum install -y mariadb mariadb-server
[root@db1 ~]# systemctl start mariadb 启动MariaDB服务
[root@db1 ~]# systemctl enable mariadb 开机自启
初始化MariaDB数据库(在db1节点)
[root@db1 ~]# mysql_secure_installation
Enter current password for root (enter for none): 默按回车
OK, successfully used password, moving on...
Set root password? [Y/n] y
New password: 输入密码
Re-enter new password:
Password updated successfully!
默按 y---n---y---y
修改数据库配置文件my.cnf (在db1 主节点上)
[root@db1 ~]# vi /etc/my.cnf
添加以下内容
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步MySQL系统数据库
server_id = 205 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写18
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent as
sorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
重启数据库
[root@db1 ~]# systemctl restart mariadb
开放主节点的数据库权限(在db1节点)
[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '000000';
Query OK, 0 rows affected (0.002 sec)
配置从节点db2同步主节点db1(在db2节点)
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.026 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: db1
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 701
Relay_Log_File: db2-relay-bin.000002
Relay_Log_Pos: 1000
Relay_Master_Log_File: mysql-bin.000001
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: 701
Relay_Log_Space: 1307
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: 205
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.001 sec)
如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。
ERROR: No query specified 出现此错误的原因是因为执行命令:show slave status\G;的时候,多加了一个“;”分号
验证主从数据库的同步功能(在db2节点)
MariaDB [(none)]> create database test; 创建库test
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> show databases; 查看库test
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.002 sec)
MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); 在库test中创建表company
Query OK, 0 rows affected (0.010 sec)
MariaDB [test]> insert into company values(1,"facebook","usa"); 插入表数据
Query OK, 1 row affected (0.003 sec)
MariaDB [test]> select * from company; 查看表company数据
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.001 sec)
查看db2的数据库(在db2节点)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.002 sec)
MariaDB [test]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.001 sec)
安装Mycat服务
[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chown -R 777 /usr/local/mycat/ 赋予目录权限
添加Mycat服务的系统变量
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile 生效变量
编辑Mycat的逻辑库配置文件
[root@mycat ~]# vim /usr/local/mycat/conf/schema.xml
可以直接删除原来schema.xml的内容,替换为如下:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="USERDB" checkSQLschema="true" sqlMaxLimi
t="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="t
t" />
<dataHost name="localhost1" maxCon="1000" minCon="10"
balance="3" dbType="mysql" dbDriver="native" writeType
="0" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="10.30.59.205:3306" u
ser="root" password="000000"> IP需要修改成实际的IP地址
<readHost host="hostS1" url="10.30.59.215:3306
" user="root" password="000000" />
</writeHost>
</dataHost>
</mycat:schema>
[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
编辑mycat的访问用户
[root@mycat ~]# vim /usr/local/mycat/conf/server.xml
在配置文件的最后部分,删除如下几行:
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
在配置文件的最后部分进行修改:
<user name="root">
<property name="password">123456</property> 修改root用户的访问密码与数据库
<property name="schemas">USERDB</property> 访问Mycat的逻辑库为USERDB
启动Mycat服务
[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start 启动Mycat数据库中间件服务
Starting Mycat-server...
[root@mycat ~]# yum install -y net-tools
[root@mycat ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 12303/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1075/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1321/master
tcp6 0 0 :::1984 :::* LISTEN 12303/java
tcp6 0 0 :::8066 :::* LISTEN 12303/java
tcp6 0 0 :::32776 :::* LISTEN 12303/java
tcp6 0 0 :::9066 :::* LISTEN 12303/java
tcp6 0 0 :::41839 :::* LISTEN 12303/java
tcp6 0 0 :::22 :::* LISTEN 1075/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1321/master
如果有开放8066和9066端口,则表示Mycat服务开启成功
用Mycat服务查询数据库信息
[root@mycat ~]# yum install -y MariaDB-client
查看Mycat服务的逻辑库USERDB
因为Mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company
[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p000000
MySQL [(none)]> show databases; 查看库
+----------+
| DATABASE |
+----------+
| USERDB |
+----------+
1 row in set (0.004 sec)
MySQL [(none)]> use USERDB
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 [USERDB]>
MySQL [USERDB]> show tables; 查看tables表
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.003 sec)
MySQL [USERDB]> select * from company; 查看表信息
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.048 sec)
用Mycat服务添加表数据
使用mysql命令对表company添加一条数据(2,“basketball”,“usa”),添加完毕后查看表信息
MySQL [USERDB]> insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (0.009 sec)
MySQL [USERDB]> select * from company; 查看表信息
+----+------------+------+
| id | name | addr |
+----+------------+------+
| 1 | facebook | usa |
| 2 | bastetball | usa |
+----+------------+------+
2 rows in set (0.002 sec)
MySQL [USERDB]> Ctrl-C -- exit!
Aborted
验证Mycat服务对数据库读写操作分离
1、使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。
2、可以看到所有的写入操作WRITE_LOAD数都在db1主数据库节点上,所有的读取操作READ_LOAD数都在db2主数据库节点上。
3、由此可见,数据库读写操作已经分离到db1和db2节点上了
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show @@datasource;'
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | hostM1 | mysql | 10.30.59.205 | 3306 | W | 0 | 10 | 1000 | 80 | 0 | 1 |
| dn1 | hostS1 | mysql | 10.30.59.215 | 3306 | R | 0 | 7 | 1000 | 79 | 4 | 0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
Mycat读写分离数据库案例完成