MHA+Lvs+Keepalived实现MySQL的高可用及读负载均衡_2(MySQL)
MySQL 一主多从半同步复制架构搭建
一、下载并安装Cmake
1、下载地址
http://www.cmake.org/cmake/resources/software.html
2、安装
[root@MySQL-15.11 xxm]# tar xf cmake-2.8.12.2.tar.gz
[root@MySQL-15.11 xxm]# cd cmake-2.8.12.2
[root@MySQL-15.11 cmake-2.8.12.2]# ./configure && make && make install
二、下载并安装配置MySQL
源码编译安装,因为MySQL的安装是一样的,以MySQL-15.11为例。
1、下载地址
http://dev.mysql.com/downloads/mysql/
MySQL Community Server ->Source code ->Generic Linux (Architecture Independent), Compressed TAR
Archive(mysql-5.5.37.tar.gz)
2、安装
2.1 创建mysql账号
[root@MySQL-15.11 xxm]# groupadd mysql
[root@MySQL-15.11 xxm]# useradd -g mysql mysql
2.2 创建相应的目录
[root@MySQL-15.11 xxm]# mkdir -p /data/log /data/tmp /data/data /var/run/mysql /usr/local/mysql
[root@MySQL-15.11 xxm]# chown -R mysql.mysql /data /var/run/mysql /usr/local/mysql
Mysql数据目录:/data/data
Mysql临时目录:/data/tmp
Mysql日志目录:/data/log
Mysql安装目录;/usr/local/mysql
Mysql进程及socket目录:/var/run/mysql
2.3 编译安装
2.3.1 编译
[root@MySQL-15.11 xxm]# tar xf mysql-5.5.37.tar.gz
[root@MySQL-15.11 xxm]# cd mysql-5.5.37
[root@MySQL-15.11 mysql-5.5.37]# cmake . -LH |more
cmake . -LH :查看支持的编译选项
[root@MySQL-15.11 mysql-5.5.37]]# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/data \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1
2.3.2 安装
[root@MySQL-15.11 mysql-5.5.37]]# make (历时15分钟)
[root@MySQL-15.11 mysql-5.5.37]]# make install
2.3.3 让MySQL的动态链接库为系统所共享
echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf
ldconfig
2.4 配置my.cnf文件
[client]
port=36677
socket=/var/run/mysql/mysql.sock
[mysqld]
port=36677
user=mysql
pid-file=/var/run/mysql/mysqld.pid
socket=/var/run/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/data/data
tmpdir=/data/tmp
server-id=2 #每个MySQL server的此选项都必须唯一
###begin innodb settiong###
innodb_file_per_table=1
innodb_lock_wait_timeout=500
innodb_buffer_pool_size=512M
###end innodb setting###
###key buffer size set###
key-buffer-size=10M
sort_buffer_size=10M
###key buffer size set###
###begin bin log###
log-bin=/data/log/log
expire-logs-days=90
###end bin log###
###begin general log###
#general_log=1
#general_log_file=/data/log/record.log
###end general log###
###begin error log###
log_error=/data/log/error.log
###end error log###
###begin skip name resolve###
skip_name_resolve=1
###end skip name resolve###
###being slow query log###
slow_query_log=1
long_query_time=0.1
slow_query_log_file=/data/log/slow.log
###end slow query log###
###begin replication config###
read_only=1 #MySQL主,此选项设为0,MySQL从,此选项设为1
relay_log_purge=0
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%
#rpl_semi_sync_slave_enabled=1
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
###end replication config###
2.5 初始化数据库
[root@MySQL-15.11 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/data
2.6 启动MySQL服务
2.6.1 mysql客户端命令
cp /etc/profile /etc/profile.bak
echo ' ' >> /etc/profile
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
2.6.2 MySQL服务端命令
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2.6.3 启动MySQL服务
#使MySQL服务开机自启动
[root@MySQL-15.11 ~]# chkconfig mysqld on
#启动MySQL服务
[root@MySQL-15.11 ~]#/etc/init.d/mysqld start
2.7 账号管理
2.7.1 删除匿名账号
mysql -e "delete from mysql.user where user=' ';delete from mysql.user where user='';flush privileges;
2.7.2 给管理员账号设密码
mysqladmin -u root password $password
2.7.3 创建复制账号
create user repl@'192.168.15.%' identified by '123456';
grant replication slave on *.* to repl@'192.168.15.%' ;
flush privileges;
2.7.3 创建MHA管理账号
create user mha_manager@'192.168.15.%' identified by '123456';
grant all on *.* to mha_manager@'192.168.15.%';
flush privileges;
2.8 半同步复制
2.8.1 安装相关插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
2.8.2 配置相关参数
#将前文提及的相关选项取消注释
/bin/sed -i 's/#rpl_semi/rpl_semi/p' /etc/my.cnf
#重启mysqld,使配置生效
/etc/init.d/mysqld restart
******到此,上述操作在4台MySQL服务器上均被执行,个别参数需注意(eg:server_id、read_only)下述2.8.3从指定主的操作仅在从服务器上执行。******
2.8.3 slave指定master
第一步:先查看Master——MySQL-15.11的二进制日志信息
[root@MySQL-15.11 ~ 10:00:27]#mysql -uroot -p123456 -e "show master status;"
+------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| log.000003 | 394 | | |
+------------+----------+--------------+------------------+
第二步:其他3台Slave指定Master,以MySQL-15.12为例
[root@MySQL-15.12 ~ 10:06:17]#mysql -uroot -p123456 -e "change master to master_host='192.168.15.11', master_user='repl', master_password='123456', master_port=36677, master_log_file='log.000003', master_log_pos=394;"
[root@MySQL-15.12 ~ 10:07:55]#mysql -uroot -p123456 -e "start slave;"
[root@MySQL-15.12 ~ 10:08:13]#mysql -uroot -p123456 -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.15.11
Master_User: repl
Master_Port: 36677
Connect_Retry: 60
Master_Log_File: log.000003
Read_Master_Log_Pos: 394
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 247
Relay_Master_Log_File: log.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
。。。。。。
Exec_Master_Log_Pos: 394
Relay_Log_Space: 404
。。。。。。
Master_Server_Id: 11
第三步:主、从上分别查看半复制状态
主——MySQL-15.11
[root@MySQL-15.11 ~ 10:14:32]#mysql -uroot -p123456 -e "show global status like 'rpl_semi_sync%';"
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 3 |
。。。。。。
| Rpl_semi_sync_master_status | ON |
。。。。。。
| Rpl_semi_sync_slave_status | OFF |
+--------------------------------------------+-------+
从——以MySQL-15.12为例
[root@MySQL-15.12 ~ 10:15:38]#mysql -uroot -p123456 -e "show global status like 'rpl_semi_sync%';"
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
。。。。。。
| Rpl_semi_sync_master_status | OFF |
。。。。。。
| Rpl_semi_sync_slave_status | ON |
+--------------------------------------------+-------+
至此,MySQL的一主三从的半同步复制架构已搭建完成,下面进行实现MySQL高可用的MHA安装及配置。
需注意的项:
1、为保证每个slave都有可能成为master,所以每个MySQL server都开启binlog日志,并设置replication账号。因为每个slave都有可能成为master,所以binlog、replication 账号、semisync的master及slave so文件安装及相关配置,所以的MySQL server都安装并配置。
2、master及slave上相关半同步复制的global variable必须在相应的so文件安装后才有效,否则设置了mysql也不认得。
Master报错:
141127 10:59:07 [ERROR] /opt/mysql/bin/mysqld: unknown variable 'rpl_semi_sync_master_enabled=1'
Slave报错:
141127 11:00:04 [ERROR] /usr/local/mysql/bin/mysqld: unknown variable 'rpl_semi_sync_slave_enabled=1'
3、在每个slave上设置read_only=1,避免应用程序对slave服务器进行write。