环境规划: 主db1 ip:192.168.1.247 host:tong1 主db2 ip:192.168.1.248 host:tong2 从db3 ip:192.168.1.249 host:tong3 monitor ip:192.168.1.249 host:tong3 数据库:mysql-5.6.21 mysqldba技术群 378190849 武汉-linux运维群 236415619 1.网络环境布置 ton
环境规划:
主db1 ip:192.168.1.247 host:tong1
主db2 ip:192.168.1.248 host:tong2
从db3 ip:192.168.1.249 host:tong3
monitor ip:192.168.1.249 host:tong3
数据库:mysql-5.6.21
mysql dba技术群 378190849
武汉-linux运维群 236415619
1.网络环境布置
tong1数据节点:
[root@tong1 ~]# ifconfig eth0
eth0 link encap:ethernet hwaddr 10:78:d2:c7:83:03
inet addr:192.168.1.247 bcast:192.168.1.255 mask:255.255.255.0
inet6 addr: fe80::1278:d2ff:fec7:8303/64 scope:link
up broadcast running multicast mtu:1500 metric:1
rx packets:4953337 errors:0 dropped:0 overruns:0 frame:0
tx packets:78512 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
rx bytes:346785301 (330.7 mib) tx bytes:5389268 (5.1 mib)
[root@tong1 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[root@tong1 ~]#
tong2数据节点:
[root@tong2 ~]# ifconfig eth0
eth0 link encap:ethernet hwaddr 10:78:d2:c7:17:e8
inet addr:192.168.1.248 bcast:192.168.1.255 mask:255.255.255.0
inet6 addr: fe80::1278:d2ff:fec7:17e8/64 scope:link
up broadcast running multicast mtu:1500 metric:1
rx packets:4930658 errors:0 dropped:0 overruns:0 frame:0
tx packets:19441 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
rx bytes:315661835 (301.0 mib) tx bytes:2133138 (2.0 mib)
[root@tong2 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[root@tong2 ~]#
tong3监控节点:
[root@tong3 ~]# ifconfig eth0
eth0 link encap:ethernet hwaddr 10:78:d2:c8:f7:50
inet addr:192.168.1.249 bcast:192.168.1.255 mask:255.255.255.0
inet6 addr: fe80::1278:d2ff:fec8:f750/64 scope:link
up broadcast running multicast mtu:1500 metric:1
rx packets:6864426 errors:0 dropped:0 overruns:0 frame:0
tx packets:99046 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
rx bytes:694563286 (662.3 mib) tx bytes:7322797 (6.9 mib)
[root@tong3 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[root@tong3 ~]#
2.在监控节点上tong3安装mysql-mmm软件
下载地址:http://mysql-mmm.org/downloads
[root@tong3 ~]# tar xvf mysql-mmm-2.2.1.tar.gz
[root@tong3 ~]# cd mysql-mmm-2.2.1
[root@tong3 mysql-mmm-2.2.1]# make && make install
[root@tong3 mysql-mmm-2.2.1]# mmm_
mmm_agentd mmm_backup mmm_clone mmm_control mmm_mond mmm_restore
[root@tong3 mysql-mmm-2.2.1]# ll /etc/mysql-mmm/
total 16
-rw-r-----. 1 root root 33 apr 29 14:06 mmm_agent.conf
-rw-r-----. 1 root root 684 apr 29 14:06 mmm_common.conf
-rw-r-----. 1 root root 321 apr 29 14:06 mmm_mon.conf
-rw-r-----. 1 root root 1293 apr 29 14:06 mmm_tools.conf
[root@tong3 mysql-mmm-2.2.1]#
3.在tong1,tong2,tong3节点安装mysql数据库(步骤一样)
[root@tong1 ~]# wget http://mirrors.sohu.com/mysql/mysql-5.6/mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
[root@tong1 ~]# tar xvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz -c /usr/local/
[root@tong1 ~]# cd /usr/local/
[root@tong1 ~]# mv mysql-5.6.23-linux-glibc2.5-x86_64/ mysql-5.6.23
[root@tong1 ~]# cd mysql-5.6.23/
[root@tong1 ~]# ./scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql-5.6.23 --datadir=/usr/local/mysql-5.6.23/data
[root@tong1 ~]# cp -a my.cnf /etc/
[root@tong1 ~]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@tong1 ~]# chkconfig --add mysqld
[root@tong1 ~]# chkconfig mysqld on
[root@tong1 ~]# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20 --server_id在三台主机不同,分别用10,20,30表示
socket = /tmp/mysql.sock
[root@tong1 ~]# pkill mysqld
[root@tong1 ~]# /etc/init.d/mysqld restart
[root@tong1 ~]# /usr/local/mysql-5.6.23/bin/mysqladmin -u root password 'system'
[root@tong1 ~]# /usr/local/mysql-5.6.23/bin/mysql -u root -p --输入密码system
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 5
server version: 5.6.23 mysql community server (gpl)
copyright (c) 2000, 2015, oracle and/or its affiliates. all rights reserved.
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mysql> exit
bye
[root@tong1 ~]#
4.将tong1和tong2配置成主主架构
tong1节点配置文件:
[root@tong1 ~]# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20
socket = /tmp/mysql.sock
replicate-do-db=tong --复制tong数据库
replicate-ignore-db=mysql --忽略mysql数据库
log-bin=mysql-bin --开启二进制日志
log-bin-index=mysql-bin-index
auto_increment_offset=1
auto_increment_increment=2
relay-log=relay-log --开启中继日志
relay-log-index=relay-log-index
log_slave_updates --当任意一台主宕机,从可以接管主应用
sync-binlog=1
[root@tong1 ~]#
tong2节点配置文件:
[root@tong2 ~]# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 10
socket = /tmp/mysql.sock
replicate-do-db=tong
replicate-ignore-db=mysql
log-bin=mysql-bin
log-bin-index=mysql-bin-index
auto_increment_offset=2
auto_increment_increment=2
relay-log=relay-log
relay-log-index=relay-log-index
log_slave_updates
sync-binlog=1
[root@tong2 ~]#
tong3节点配置文件:
[root@tong3 ~]# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 30
socket = /tmp/mysql.sock
replicate-do-db=tong
replicate-ignore-db=mysql
[root@tong3 ~]#
配置主主模式:
tong1节点:
[root@tong1 ~]# /etc/init.d/mysqld restart
shutting down mysql.... success!
starting mysql. success!
[root@tong1 ~]# mysql -u root -p
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 2
server version: 5.6.23-log mysql community server (gpl)
copyright (c) 2000, 2015, oracle and/or its affiliates. all rights reserved.
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.%' identified by 'system!#%246'; --创建复制用户
query ok, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
tong2节点:
[root@tong2 ~]# mysql -u root -p
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 2
server version: 5.6.23-log mysql community server (gpl)
copyright (c) 2000, 2015, oracle and/or its affiliates. all rights reserved.
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.%' identified by 'system!#%246';
query ok, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.247',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000001',master_log_pos=120; --复制tong1中的数据
query ok, 0 rows affected, 2 warnings (0.50 sec)
mysql> start slave; --开启从服务
query ok, 0 rows affected (0.05 sec)
mysql>
tong1节点:
mysql> change master to master_host='192.168.1.248',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000001',master_log_pos=120; --复制tong2中的数据
query ok, 0 rows affected, 2 warnings (0.22 sec)
mysql> start slave;
query ok, 0 rows affected (0.05 sec)
mysql> show slave status\g
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.1.248
master_user: repl_user
master_port: 3306
connect_retry: 60
master_log_file: mysql-bin.000001
read_master_log_pos: 120
relay_log_file: relay-log.000002
relay_log_pos: 283
relay_master_log_file: mysql-bin.000001
slave_io_running: yes --主主同步成功
slave_sql_running: yes
replicate_do_db: tong
replicate_ignore_db: mysql
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: 120
relay_log_space: 450
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: 10
master_uuid: de5d22d1-ed4b-11e4-9390-1078d2c717e8
master_info_file: /usr/local/mysql-5.6.23/data/master.info
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
master_retry_count: 86400
master_bind:
last_io_error_timestamp:
last_sql_error_timestamp:
master_ssl_crl:
master_ssl_crlpath:
retrieved_gtid_set:
executed_gtid_set:
auto_position: 0
1 row in set (0.00 sec)
mysql>
5.测试主主架构是否生效
tong1节点:
mysql> create table a(a int);
query ok, 0 rows affected (0.39 sec)
mysql> insert into a values(1);
query ok, 1 row affected (0.03 sec)
mysql> select * from a;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
tong2节点:
mysql> \u tong
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> select * from a;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> insert into a values(2);
query ok, 1 row affected (0.03 sec)
mysql> select * from a;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql>
6.将tong3设置成tong1节点的从机
[root@tong3 ~]# /etc/init.d/mysqld restart
shutting down mysql.. success!
starting mysql. success!
[root@tong3 ~]# mysql -u root -p
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 16
server version: 5.6.23 mysql community server (gpl)
copyright (c) 2000, 2015, oracle and/or its affiliates. all rights reserved.
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.1.247',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000001',master_log_pos=120; --复制tong1中的数据
query ok, 0 rows affected, 2 warnings (0.27 sec)
mysql> start slave;
query ok, 0 rows affected (0.07 sec)
mysql> \u tong
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> select * from a; --数据已同步
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql>
7.在三个数据库节点分别创建监控用户和代理用户
mysql> grant replication client on *.* to 'mmm_moniton'@'192.168.1.%' identified by 'moniton';
query ok, 0 rows affected (0.00 sec)
mysql> grant super,replication client,process on *.* to 'mmm_agent'@'192.168.1.%' identified by 'agent';
query ok, 0 rows affected (0.00 sec)
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
mysql>
8.在监控节点修改配置文件
[root@tong3 ~]# cd /etc/mysql-mmm/
[root@tong3 mysql-mmm]# ll
total 16
-rw-r-----. 1 root root 33 apr 29 14:06 mmm_agent.conf
-rw-r-----. 1 root root 684 apr 29 14:06 mmm_common.conf
-rw-r-----. 1 root root 321 apr 29 14:06 mmm_mon.conf
-rw-r-----. 1 root root 1293 apr 29 14:06 mmm_tools.conf
[root@tong3 mysql-mmm]# vim mmm_common.conf
active_master_role writer
cluster_interface eth0 --心跳网卡接口
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm
replication_user repl_user --复制用户名和密码
replication_password system!#%246
agent_user mmm_agent --代理用户名和密码
agent_password agent
--主机名
ip 192.168.1.247 --tong1的ip地址
mode master --主模式
peer tong1 --主机名
ip 192.168.1.248
mode master
peer tong2
ip 192.168.1.249
mode slave --从服务器
hosts tong1, tong2 --tong1和tong2可写
ips 192.168.1.120
mode exclusive
hosts tong1, tong2, tong3 --三台可读
ips 192.168.1.121
mode balanced
[root@tong3 mysql-mmm]# vim mmm_mon.conf
include mmm_common.conf
ip 127.0.0.1
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.1.247, 192.168.1.248, 192.168.1.249
auto_set_online 10
monitor_user mmm_moniton --监控用户名和密码
monitor_password moniton
debug 1 --为1是开启服务打印日志,为0是只开启服务
[root@tong3 mysql-mmm]# cat mmm_agent.conf
include mmm_common.conf
this tong3 --主机名
[root@tong3 mysql-mmm]#
9.将tong3节点的mmm_common.conf复制到tong1和tong2节点中
[root@tong3 mysql-mmm]# scp mmm_common.conf tong1:/etc/mysql-mmm/
mmm_common.conf 100% 674 0.7kb/s 00:00
you have mail in /var/spool/mail/root
[root@tong3 mysql-mmm]# scp mmm_common.conf tong2:/etc/mysql-mmm/
mmm_common.conf 100% 674 0.7kb/s 00:00
[root@tong3 mysql-mmm]#
tong1,tong2修改mmm_agent.conf文件并启动服务
[root@tong1 mysql-mmm]# cat mmm_agent.conf
include mmm_common.conf
this tong1 --修改主机名
[root@tong1 mysql-mmm]# /etc/init.d/mysql-mmm-agent restart --重启服务
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
shutting down mmm agent daemon. ok
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
starting mmm agent daemon... ok
[root@tong1 mysql-mmm]#
tong3启动mmm-mysql-agent和mysql-mmm-monitor服务
[root@tong3 mysql-mmm]# /etc/init.d/mysql-mmm-agent restart
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
shutting down mmm agent daemon. ok
daemon bin: '/usr/sbin/mmm_agentd'
daemon pid: '/var/run/mmm_agentd.pid'
starting mmm agent daemon... ok
[root@tong3 mysql-mmm]# /etc/init.d/mysql-mmm-monitor restart
daemon bin: '/usr/sbin/mmm_mond'
daemon pid: '/var/run/mmm_mond.pid'
daemon bin: '/usr/sbin/mmm_mond'
daemon pid: '/var/run/mmm_mond.pid'
shutting down mmm monitor daemon: not running.
daemon bin: '/usr/sbin/mmm_mond'
daemon pid: '/var/run/mmm_mond.pid'
starting mmm monitor daemon: ok
[root@tong3 mysql-mmm]# mmm_control show
tong1(192.168.1.247) master/online. roles: writer(192.168.1.120)
tong2(192.168.1.248) master/online. roles:
tong3(192.168.1.249) slave/online. roles: reader(192.168.1.121)
[root@tong3 mysql-mmm]# mmm_control set_offline tong1
ok: state of 'tong1' changed to admin_offline. now you can wait some time and check all roles!
[root@tong3 mysql-mmm]# mmm_control show
tong1(192.168.1.247) master/admin_offline. roles:
tong2(192.168.1.248) master/online. roles: writer(192.168.1.120)
tong3(192.168.1.249) slave/online. roles: reader(192.168.1.121)
[root@tong3 mysql-mmm]# mmm_control set_offline tong3
ok: state of 'tong3' changed to admin_offline. now you can wait some time and check all roles!
[root@tong3 mysql-mmm]# mmm_control show
tong1(192.168.1.247) master/admin_offline. roles:
tong2(192.168.1.248) master/online. roles: reader(192.168.1.121), writer(192.168.1.120)
tong3(192.168.1.249) slave/admin_offline. roles:
[root@tong3 mysql-mmm]# mmm_control set_online tong1
ok: state of 'tong1' changed to online. now you can wait some time and check its new roles!
[root@tong3 mysql-mmm]# mmm_control set_online tong3
ok: state of 'tong3' changed to online. now you can wait some time and check its new roles!
[root@tong3 mysql-mmm]# mmm_control show
tong1(192.168.1.247) master/online. roles: reader(192.168.1.121)
tong2(192.168.1.248) master/online. roles: writer(192.168.1.120)
tong3(192.168.1.249) slave/online. roles:
you have mail in /var/spool/mail/root
[root@tong3 mysql-mmm]