先修改配置文件,添加常用的几项
主节点服务器
vim /etc/my.cof.d/server.cnf
[mysqld]
innodb_file_per_table=ON
skip_name_resolve=ON
server_id=1
log-bin=master-log
然后在从节点服务器上配置
[mysqld]
innodb_file_per_table=ON
skip_name_resolve=ON
server_id=11
relay_log=relay-log
read_only=ON
注意:从服务器的mysql 版本可以比主节点的高,但不能倒过来
然后就可以启动服务了
在主节点上授权从节点过来拉取二进制日志的用户权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.31.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
然后查看当前的LSN的位置,从当前位置开始复制即可
mysql> SHOW MASTER STATUS;
然后在从节点连接到主节点上
CHANGE MASTER TO MASTER_HOST='192.168.31.203',MASTER_USER='repluser',MASTER_PASSWORD='replpass',
MASTER_PORT=3306,MASTER_LOG_FILE='master-log.000002',MASTER_LOG_POS=320;
可以查看从节点状态
SHOW SLAVE STATUS;
没有问题的话,接下来我们就可以启动线程了
从节点上有两个线程
IO_THREAD | SQL_THREAD
一个是从主节点上拉取二进制日志的线程,一个是将中继日志中的数据恢复到数据库中的线程这里我们两个都启动
START SLAVE;
但是启动报错
[root@lvq-7-4-2 ~]# tail /var/log/mariadb/mariadb.log
181214 16:41:52 [ERROR] Failed to open the relay log './mariadb-relay-bin.000003' (relay_log_pos 408)
181214 16:41:52 [ERROR] Could not find target log during relay log initialization
181214 16:41:52 [ERROR] Failed to initialize the master info structure
181214 16:41:52 [Note] Event Scheduler: Loaded 0 events
181214 16:41:52 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.56-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaDB Server
181214 16:42:16 [ERROR] Failed to open the relay log './mariadb-relay-bin.000003' (relay_log_pos 408)
181214 16:42:16 [ERROR] Could not find target log during relay log initialization
181214 16:48:39 [ERROR] Failed to open the relay log './mariadb-relay-bin.000003' (relay_log_pos 408)
181214 16:48:39 [ERROR] Could not find target log during relay log initialization
通过网上查找答案发现是在从服务器设置的时候多次输入相同的命令,导致数据库目录下生成了多余的文件干扰
删除 /var/lib/mysql/路径下 ‘master.info’ ‘mysqld-relay-bin.*’ ‘relay-log.info’ ‘relay-log-index.*’
rm -rf ./master.info,rm -rf *relay*
接下来我们演示基于Mysql Proxy中间件的使用,计划一台Proxy,一台主节点,两台从节点。新加的从节点没有数据,先从主节点备份过来
mysqldump -uroot --all-databases -R -E --triggers -x --master-data=2 > all.sql
scp all.sql root@192.168.31.204:/root
systemctl start mariadb
mysql < all.sql
less all.sql #查看是从哪个位置开始复制的
CHANGE MASTER TO MASTER_HOST='192.168.31.203',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000001',MASTER_LOG_POS=732;
接下来在读写分离主机上安装ProxySql
首先需要数据库授权一个账号给ProxySql,因为proxysql也是后端mysql的客户端。在主节点上授权从服务器上都会复制过去
GRANT ALL ON *.* TO 'myadmin'@'192.168.31.%' IDENTIFIED BY 'mypass';
FLUSH PRIVILEGES;
配置文件
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin"
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
}
mysql_variables=
{
threads=4
max_connections=2048
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:3306;/tmp/mysql.sock"
default_schema="information_schema"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
monitor_history=600000
monitor_connect_interval=60000
monitor_ping_interval=10000
monitor_read_only_interval=1500
monitor_read_only_timeout=500
ping_interval_server=120000
ping_timeout_server=500
commands_stats=true
sessions_sort=true
connect_retries_on_failure=10
}
mysql_servers =
(
{
address = "192.168.31.203" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 0 # no default, required
weight = 1 # default: 1
compression = 0 # default: 0
},
{
address = "192.168.31.201"
port = 3306
hostgroup = 1
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
},
{
address = "192.168.31.204"
port = 3306
hostgroup = 1
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
}
)
mysql_users:
(
{
username = "myadmin"
password = "mypass"
default_hostgroup = 0
max_connections=1000
default_schema="mydb"
active = 1
}
)
mysql_query_rules:
(
)
scheduler=
(
)
mysql_replication_hostgroups=
(
{
writer_hostgroup=0
reader_hostgroup=1
}
)
启动服务然后连接数据库
mysql -h192.168.31.200 -umyadmin -pmypass
然后进行实验,创建一张表,如果主从节点上都有了,就证明操作被路由到了主节点上
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1 |
| tbl2 |
| tbl3 |
| tbl5 |
| tbl7 |
+----------------+
5 rows in set (0.01 sec)
mysql> CREATE TABLE tbl8(id INT)
-> ;
Query OK, 0 rows affected (0.16 sec)
主节点上:
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1 |
| tbl2 |
| tbl3 |
| tbl5 |
| tbl7 |
| tbl8 |
+----------------+
从节点上:
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1 |
| tbl2 |
| tbl3 |
| tbl5 |
| tbl7 |
| tbl8 |
+----------------+
6 rows in set (0.00 sec)
使用命令SHOW PROCESSLIST\G;
可以查看后端有几台服务器连接中
ProxySql还有管理接口可以使用命令连接mysql -uadmin -padmin -hlocalhost -S /tmp/proxysql_admin.sock
ProxySql支持动态配置可以修改数据库的表,四个库的表都是一样的内容
mysql> SHOW TABLES;
+--------------------------------------+
| tables |
+--------------------------------------+
| global_variables |
| mysql_collations |
| mysql_query_rules |
| mysql_replication_hostgroups |
| mysql_servers |
| mysql_users |
| runtime_global_variables |
| runtime_mysql_query_rules |
| runtime_mysql_replication_hostgroups |
| runtime_mysql_servers |
| runtime_mysql_users |
| runtime_scheduler |
| scheduler |
+--------------------------------------+
但这时候我们不得不考虑一个问题,如果后端的主节点DOWN掉了怎么办呢,就算一台从节点升为主节点,但可能与其他的从节点数据不一致,这时候就用到了MHA
MHA工作在单独的一台服务器上manager,每一台主从服务器都有一个node帮助manager管理,一但一台主机down掉了,node会在最新的从节点升为主节点之前将所有节点的数据同步过来,合并起来,避免数据不一致,同时需要无密钥SSH登陆四台主机
这里我们直接使用ProxySql主机来做MHA的manager
首先要修改各节点的mysql配置文件,因为每个节点都有可能成/为主/从节点,所以都要启用二进制日志和中继日志
修改主节点配置文件添加
relay_log=relay-log
修改从节点配置文件添加
log_bin=master-log
relay_log_purge=0
read_only=1
接下来配置SSH
在manager节点上
ssh-keygen -t rsa -P ''
先复制给本机,再复制给其他主机,这样用的就都是同一种密钥
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.31.200
scp -p .ssh/authorized_keys .ssh/id_rsa{,.pub} root@192.168.31.201:/root/.ssh/
scp -p .ssh/authorized_keys .ssh/id_rsa{,.pub} root@192.168.31.203:/root/.ssh/
scp -p .ssh/authorized_keys .ssh/id_rsa{,.pub} root@192.168.31.204:/root/.ssh/
然后在manager上安装
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
在node上安装
mha4mysql-node-0.56-0.el6.noarch.rpm
全局配置文件默认为/etc/masterha_default.cnf默认不存在
也可通过application的配置来提供各服务器的默认配置信息,每个application的配置文件路径自定义,这里我们使用/etc/masterha/app1.cnf
先在主节点创建一个用户用于manager管理,使用proxysql的也可以
MariaDB [(none)]> GRANT ALL ON *.* TO 'mhaadmin'@'192.168.31.%' IDENTIFIED BY 'mhapass';
MariaDB [(none)]> FLUSH PRIVILEGES;
配置文件
[server default]
user=mhaadmin
password=mhapass
manager_workdir=/data/masterha/app1
manager_log=/data/masterha/app1/manager.log
remote_workdir=/data/masterha/app1
ssh_use=root
repl_user=repluser
repl_pas
sword=replpass
ping_interval=1
[server1]
hostname=192.168.31.201
candidate_master=1
[server2]
hostname=192.168.31.203
candidate_master=1
[server3]
hostname=192.168.31.204
candidate_master=1
接下来测试通信是否正常
masterha_check_ssh --conf=/etc/masterha/app1.cnf
测试主从复制健康状态
masterha_check_repl --conf=/etc/masterha/app1.cnf
报故障
[error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln622] Master 192.168.31.200:3306 from which slave 192.168.31.203(192.168.31.203:3306)
replicates is not defined in the configuration file!
网上找到原因是由于新master此时还是存有指向老master的slave状态的,所以mha将新master当成了一个slave,而新master的slave所指向的是旧的且已经宕机的老master,所以报错,解决办法是清除新master的salve信息。
STOP SLAVE;
RESET SLAVE ALL;
又报错
User repluser does not exist or does not have REPLICATION SLAVE privilege!
Other slaves can not start replication from this host.
原因应该是repluser在创建时没有同步,从新创建一个用户
GRANT ALL ON *.* TO 'repladmin'@'192.168.31.%' IDENTIFIED BY 'replpass';
FLUSH PRIVILEGES;
vim /etc/masterha/app1.cnf
检测成功了
Sat Dec 15 02:36:48 2018 - [info] Checking replication health on 192.168.31.201..
Sat Dec 15 02:36:48 2018 - [info] ok.
Sat Dec 15 02:36:48 2018 - [info] Checking replication health on 192.168.31.204..
Sat Dec 15 02:36:48 2018 - [info] ok.
Sat Dec 15 02:36:48 2018 - [warning] master_ip_failover_script is not defined.
Sat Dec 15 02:36:48 2018 - [warning] shutdown_script is not defined.
Sat Dec 15 02:36:48 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
接下来运行mha
nohup masterha_manager --conf=/etc/masterha/app1.cnf &> /data/masterha/app1/manager.log &
检测状态
[root@lvq-node1 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:21503) is running(0:PING_OK), master:192.168.31.203
停止
masterha_stop --conf=/etc/masterha/app1.cnf
一但故障了,从节点转变成主节点后,主节点修复了想要从新加入
1、先将现在的主节点用mysqldump全量备份
2、用备份恢复
3、设置为从节点指向主节点