1、配置MySQL(1+2)
在安装mysql数据库时,系统会提示设置你的root密码,例如我的root密码为root。
设置my.cnf
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
重启mysql服务
连接mysql,设置远程root用户连接和删除空用户
mysql -uroot -p
grant all privileges on *.* to root@"%" identified by "root" with grant option;
FLUSH PRIVILEGES;
delete from mysql.user where user="";
quit;
停止mysql服务
2、将mysql生成的数据文件迁移到drbd0设备去(1)
drbdadm primary mysql
drbd-overview
mount /dev/drbd0 /data/mysql
cp -R -p /var/lib/mysql /data/mysql
3、由于数据文件发生变化,需要修改相应的配置文件(1+2)
sed -i 's/2345/!0123456/g' /etc/init/mysql.conf
sed -i -e '/datadir/cdatadir = \/data\/mysql\/mysql' /etc/mysql/my.cnf
sed -i '/\/var\/lib\/mysql\/ /c\/data\/mysql\/mysql\/ r,' /etc/apparmor.d/usr.sbin.mysqld
sed -i '/\/var\/lib\/mysql\/\*\* rwk/c\/data\/mysql\/mysql\/** rwk,' /etc/apparmor.d/usr.sbin.mysqld
4、测试(1)
service mysql start mysql -uroot -p CREATE DATABASE test1;
quit
service mysql stop
umount /dev/drbd0
drbdadm secondary mysql
5、测试(2)
查看数据库中是否包括了新创建的test1
drbdadm primary mysql
mount /dev/drbd0 /mydata/
service mysql start
mysql -uroot -proot -e 'show databases;'
service mysql stop
umount /dev/drbd0
drbdadm secondary mysql
6、设置mysql1为primary(1)
7、停止drbd服务(1+2)
8、设置pacemaker(1)
root@mysql1:~# crm configure
crm(live)configure# property stonith-enabled=false
crm(live)configure# commit
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# commit
crm(live)configure# primitive mysqldrbd ocf:heartbeat:drbd params drbd_resource=mysql op start timeout=240 op stop timeout=100 op monitor role=Master interval=20 timeout=30 op monitor role=Slave interval=30 timeout=30
crm(live)configure# ms ms_mysqldrbd mysqldrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
crm(live)configure# commit
crm(live)configure# exit
bye
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:10:37 2015
Last change: Fri Aug 14 09:10:28 2015 via cibadmin on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
2 Resources configured
Online: [ mysql1 mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql2 ]
Slaves: [ mysql1 ]
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:11:16 2015
Last change: Fri Aug 14 09:10:28 2015 via cibadmin on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
2 Resources configured
Online: [ mysql1 mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql2 ]
Slaves: [ mysql1 ]
root@mysql1:~# crm configuate
crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/data/mysql fstype=ext4 op start timeout=60 op stop timeout=60
crm(live)configure# colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master
crm(live)configure# order mystore_after_ms_mysqldrbd mandatory: ms_mysqldrbd:promote mystore:start
crm(live)configure# commit
crm(live)configure# exit
bye
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:13:32 2015
Last change: Fri Aug 14 09:13:24 2015 via cibadmin on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
3 Resources configured
Online: [ mysql1 mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql2 ]
Slaves: [ mysql1 ]
mystore (ocf::heartbeat:Filesystem): Started mysql2
root@mysql1:~# crm configuate
crm(live)configure# primitive mysql lsb:mysql
crm(live)configure# colocation mysql_with_mystore inf: mysql mystore
crm(live)configure# order mysql_after_mystore mandatory: mystore mysql
crm(live)configure# commit;
ERROR: syntax: commit;
crm(live)configure# commit
crm(live)configure# exit
bye
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:14:50 2015
Last change: Fri Aug 14 09:14:45 2015 via cibadmin on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
4 Resources configured
Online: [ mysql1 mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql2 ]
Slaves: [ mysql1 ]
mystore (ocf::heartbeat:Filesystem): Started mysql2
mysql (lsb:mysql): Started mysql2
root@mysql1:~# crm configuate
crm(live)configure# primitive vip ocf:heartbeat:IPaddr params ip=192.168.12.200 nic=eth0 cidr_netmask=255.255.254.0
crm(live)configure# colocation vip_with_ms_mysqldrbd inf: ms_mysqldrbd:Master vip
crm(live)configure# commit
crm(live)configure# quit
bye
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:16:03 2015
Last change: Fri Aug 14 09:15:54 2015 via cibadmin on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
5 Resources configured
Online: [ mysql1 mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql1 ]
Slaves: [ mysql2 ]
mystore (ocf::heartbeat:Filesystem): Started mysql1
mysql (lsb:mysql): Started mysql1
vip (ocf::heartbeat:IPaddr): Started mysql1
9、客户端验证(1或者2)
root@mysql1:~# mysql -uroot -p -h 192.168.12.200
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.44-0ubuntu0.14.04.1 (Ubuntu)
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> show databases; +--------------------+
| Database | +--------------------+
| information_schema | | mysql | | performance_schema |
| test1 | +--------------------+
4 rows in set (0.01 sec)
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)
mysql> show databases; +--------------------+
| Database | +--------------------+
| information_schema | | mysql | | performance_schema |
| test1 |
| test2 | +--------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
10、故障模拟
root@mysql1:~# crm
crm(live)# node
crm(live)node# show
mysql2(1084755047): normal
mysql1(1084755045): normal
crm(live)node# standby
crm(live)node# show
mysql2(1084755047): normal
mysql1(1084755045): normal
standby: on
crm(live)node# exit
bye
root@mysql1:~# crm status
Last updated: Fri Aug 14 09:17:52 2015
Last change: Fri Aug 14 09:17:34 2015 via crm_attribute on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
5 Resources configured
Node mysql1 (1084755045): standby
Online: [ mysql2 ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ mysql2 ]
Stopped: [ mysql1 ]
mystore (ocf::heartbeat:Filesystem): Started mysql2
mysql (lsb:mysql): Started mysql2
vip (ocf::heartbeat:IPaddr): Started mysql2
我们可以看到,将mysql1设置为Standby之后,mysql已经运行在mysql2上了。