环境:redhat6.9
mysql版本:mysql-5.6.41-linux-glibc2.12-x86_64
文件下载链接:链接:https://pan.baidu.com/s/1DO1J6EsajqMIsYaXPB5Ksw 提取码:k2bf
单机安装:
1.根据实际生产环境配置的,我的mysql是离线安装的,并且安装目录位于instll_mysql.sh脚本的当前目录,如脚本放置在/home下面,执行instll_mysql.sh就会将mysql安装在/home路径下面。
2.执行instll_mysql.sh脚本之后直接进入mysql的bin目录下面执行:
mysqld_safe & 就可以启动mysql的程序
3.执行完之后在mysql的bin下面执行./mysql -uroot -p(初始root密码是空的,不需要输入就可以了)
4.如果你遇到了/tmp/mysql.sock类型的连接报错,不要着急,只需要用:
ps -ef|grep mysql找到mysql的mysql.sock的位置,然后ln -s 离线的mysql.sock位置 /tmp/mysql.sock 就可以连接上mysql的程序了
主主热备
实现方式:MM+keepalived
实验部署环境介绍:
172.30.161.123 主 ---masterA
172.30.161.124 备库---masterB
都是干净环境没有任何数据
my.cnf_masterA.cnf以及my.cnf_masterB.cnf的内容将mysql,information_schema这两个基础的数据库排除热备,如果你需要热备这两个库将 binlog-ignore-db = mysql,information_schema 这个配置参数移除即可!!!
masterA(172.30.161.123):
1.将刚刚离线安装的mysql目录下面的my.cnf完全替换成my.cnf_masterA.cnf的内容即可,重新启动mysql
grant replication slave on *.* to 'bak'@'172.30.161.%' identified by '123456'; flush privileges; CHANGE MASTER TO MASTER_HOST='172.30.161.124',MASTER_USER='bak',MASTER_PASSWORD='123456',master_auto_position=1; start slave;
查看主从同步状态:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.30.161.124 Master_User: bak Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 227 Relay_Log_File: mysqld-relay-bin.000005 Relay_Log_Pos: 303 Relay_Master_Log_File: mysql-bin.000005 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: 227 Relay_Log_Space: 742 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: 2 Master_UUID: ba98ee4f-ebd9-11e8-9f77-005056a6ef2d Master_Info_File: /apps/mysql/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: ba98ee4f-ebd9-11e8-9f77-005056a6ef2d:1-3 Executed_Gtid_Set: 44c493e6-ebd9-11e8-9f74-005056a6431c:1-4, ba98ee4f-ebd9-11e8-9f77-005056a6ef2d:1-3 Auto_Position: 1 1 row in set (0.00 sec) ERROR: No query specified
ServerB(172.30.161.124):
1.将刚刚离线安装的mysql目录下面的my.cnf完全替换成my.cnf_masterB.cnf的内容即可,重新启动mysql
2.执行下面的语句:
grant replication slave on *.* to 'bak'@'172.30.161.%' identified by '123456'; flush privileges; CHANGE MASTER TO MASTER_HOST='172.30.161.123',MASTER_USER='bak',MASTER_PASSWORD='123456',master_auto_position=1; start slave;
查看主从同步状态:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.30.161.123 Master_User: bak Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 227 Relay_Log_File: mysqld-relay-bin.000005 Relay_Log_Pos: 389 Relay_Master_Log_File: mysql-bin.000003 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: 227 Relay_Log_Space: 669 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: 1 Master_UUID: 44c493e6-ebd9-11e8-9f74-005056a6431c Master_Info_File: /apps/mysql/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: 44c493e6-ebd9-11e8-9f74-005056a6431c:1-4 Executed_Gtid_Set: 44c493e6-ebd9-11e8-9f74-005056a6431c:1-4, ba98ee4f-ebd9-11e8-9f77-005056a6ef2d:1-3 Auto_Position: 1 1 row in set (0.00 sec) ERROR: No query specified
只要两机器的
Slave_IO_Running: Yes
Slave_SQL_Running: Yes都是yes说明机器的主主备份已经配置完毕了
测试一下:
在serverA 172.30.161.123上创建
mysql> create database test_db;
mysql> use test_db;
mysql> CREATE TABLE test_tb(id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,name CHAR(30));在serverB 172.30.161.124上查看结果:
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_tb |
+-------------------+
serverB 172.30.161.124 自动创建test_db数据库和 test_tb表,即主从复制成功。再验证主主复制
在serverA 172.30.161.123上插入数据:
mysql> INSERT INTO test_tb (name) VALUES ('11'),('22'),('33');
在serverB 172.30.161.124插入数据:
mysql> INSERT INTO test_tb (name) VALUES ('88'),('88'),('88');分别查看两个服务器数据:
mysql> SELECT * FROM test_tb;
+----+------+
| id | name |
+----+------+
| 1 | 11 |
| 3 | 22 |
| 5 | 33 |
| 6 | 88 |
| 8 | 88 |
| 10 | 88 |
+----+------+主主同步即为成功安装
keepalived
部署环境:
172.30.161.123 主库 ---masterA
172.30.161.124 备库---masterB
都是干净环境没有任何数据
VIP:172.30.161.126
1)安装
yum install -y gcc make openssl openssl-devel
如果你没有办法yum之类的安装手段可以通过下载需要用到的rpm包可以可以的,推荐一个博主经常下载的redhat6系列的rpm下载链接(Centos6同样适用):
http://mirrors.163.com/centos/6/os/x86_64/Packages/
因为内网环境 wget是没有办法使用的,所以我选择从网上先下载到本地之后scp到远程的服务器上,本篇文章的开头的分享链接中同样分享了本次keepalived的安装程序
tar -xvf keepalived-1.2.12.tar.gz cd keepalived-1.2.12 ./configure --prefix=/usr/local/keepalived make && make install
2)配置
$ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ $ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ $ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ $ cp -rf /usr/local/keepalived/etc/keepalived /etc
A)MasterA(172.30.161.123):
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/etc/keepalived/chk_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { interface eth1 state BACKUP priority 101 virtual_router_id 52 garp_master_delay 1 authentication { auth_type PASS auth_pass Hc8scrRddsLsc3 } virtual_ipaddress { 172.30.161.126 } track_script { chk_haproxy } #notify_master "/etc/keepalived/Mailnotify.py backup" #notify_backup "/etc/keepalived/Mailnotify.py master" #notify_fault "/etc/keepalived/Mailnotify.py fault" }
B)Slave(172.30.161.124):
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/etc/keepalived/chk_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { interface eth1 state BACKUP priority 100 virtual_router_id 52 garp_master_delay 1 authentication { auth_type PASS auth_pass Hc8scrRddsLsc3 } virtual_ipaddress { 172.30.161.126 } track_script { chk_haproxy } notify_master "/etc/keepalived/Mailnotify.py backup" notify_backup "/etc/keepalived/Mailnotify.py master" #notify_fault "/etc/keepalived/Mailnotify.py fault" }
C)主备两边都需要的检测脚本
#keepalive调用该脚本检测haproxy是否停止 $ vi /etc/keepalived/chk_haproxy.sh #!/bin/bash status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l) if [ "${status}" = "0" ]; then /etc/init.d/haproxy start status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l) if [ "${status2}" = "0" ]; then /etc/init.d/keepalived stop fi fi
3)运行
$ /etc/init.d/keepalived start $ chkconfig keepalived on
4)检查
$ ps aux|grep keeepalived #查看进程 $ ip addr show #用来查看地址是否切换的
在172.30.161.123上查看结果:
安装完成,数据库连接用172.30.161.126即可,即使一台机器宕机了也能尽可能的保证数据库可以写