标签:mysql主主 mysql主备 keepalived+mysql
http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.27-1.el6.x86_64.rpm-bundle.tar 测试环境: 两台服务器 MYSQL-1:10.11.22.78 MYSQL-2:10.11.22.248 测试系统 Centos 6.5_64 Mysql版本 MySQL-5.6.27 Keepalived版本 keepalived-1.2.13 1、安装Mysql5.6.27,两台服务器执行以下同样操作 1.1、检查系统是否安装Mysql,如有安装其他的版本卸载之 [root@MYSQL-1 ~]# rpm -qa|grep mysql* mysql-libs-5.1.71-1.el6.x86_64 [root@MYSQL-1 ~]# yum -y remove mysql-libs-* 1.2、安装Mysql所依赖的组件 [root@MYSQL-1 ~]# yum -y install perl libaio libnuma* 1.3、解压Mysql软件包并安装 [root@MYSQL-1 ~]# tar xf MySQL-5.6.27-1.el6.x86_64.rpm-bundle.tar [root@MYSQL-1 ~]# rpm -ivh MySQL-client-5.6.27-1.el6.x86_64.rpm [root@MYSQL-1 ~]# rpm -ivh MySQL-server-5.6.27-1.el6.x86_64.rpm 1.4、初始化数据并测试启动后关闭服务 [root@MYSQL-1 ~]# /usr/bin/mysql_install_db [root@MYSQL-1 ~]# service mysql start Starting MySQL. SUCCESS! [root@MYSQL-1 ~]# service mysql stop Shutting down MySQL.. SUCCESS! 1.5、移动Mysql到/data/mysql [root@MYSQL-1 ~]# mkdir /data [root@MYSQL-1 ~]# mv /var/lib/mysql /data/ 1.6、修改Mysql配置文件,添加如下内容, [root@MYSQL-1 ~]# vi /usr/my.cnf [mysqld] datadir=/data/mysql socket=/data/mysql/mysql.sock #在配置文件中添加两行 [root@MYSQL-1 ~]# service mysql start Starting MySQL. SUCCESS! 1.7、创建链接文件,不然登录数据库会报错: [root@MYSQL-1 ~]# mkdir /var/lib/mysql [root@MYSQL-1 ~]# ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock 1.8、查看数据库root用户的临时密码 [root@MYSQL-1 ~]# cat /root/.mysql_secret # The random password set for the root user at Thu Apr 21 13:06:24 2016 (local time): xJYL3JTDc0uYZipg 1.9、使用临时密码登录数据库,并修改密码为root [root@MYSQL-2 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.27 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> mysql> set password =password(‘root‘); Query OK, 0 rows affected (0.00 sec) 1.10、添加开机启动 [root@MYSQL-1 ~]# chkconfig mysql on 2、配置Mysql服务 2.1、在mysql-1服务器10.11.22.78上操作如下 [root@MYSQL-1 ~]# mysql -uroot -p mysql> CREATE USER ‘test‘@‘%‘; mysql> GRANT REPLICATION SLAVE ON *.* TO ‘test‘@‘%‘ IDENTIFIED BY ‘123‘; [root@MYSQL-1 ~]# vi /usr/my.cnf server-id = 1 log-bin = mysql-bin auto-increment-increment= 2 auto-increment-offset = 1 [root@MYSQL-1 ~]# mysql -uroot -p mysql> CHANGE MASTER TO -> MASTER_HOST=‘10.11.22.248‘, -> MASTER_USER=‘test‘, -> MASTER_PASSWORD=‘123‘; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> START SLAVE; Query OK, 0 rows affected (0.02 sec) mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.11.22.248 Master_User: test34 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: MYSQL-1-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 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: 120 Relay_Log_Space: 458 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: 29bd0cab-077f-11e6-83be-000c29933162 Master_Info_File: /data/mysql/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) 2.2、在mysql-2服务器10.11.22.248上操作如下 [root@MYSQL-2 ~]# mysql -uroot -p mysql> CREATE USER ‘test‘@‘10.11.22.78‘; mysql> GRANT REPLICATION SLAVE ON *.* TO ‘test‘@‘10.11.22.78‘ IDENTIFIED BY ‘123‘; [root@MYSQL-2 ~]# vi /usr/my.cnf server-id = 2 log-bin = mysql-bin #开启日志功能 auto-increment-increment= 2 auto-increment-offset = 1 [root@MYSQL-2 ~]# mysql -uroot -p mysql> CHANGE MASTER TO -> MASTER_HOST=‘10.11.22.78‘, -> MASTER_USER=‘test‘, -> MASTER_PASSWORD=‘123‘; Query OK, 0 rows affected, 2 warnings (0.10 sec) mysql> START SLAVE; Query OK, 0 rows affected (0.01 sec) mysql> START SLAVE; Query OK, 0 rows affected (0.01 sec) mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.11.22.78 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: MYSQL-2-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 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: 120 Relay_Log_Space: 458 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: f3a1ded7-079d-11e6-8487-000c29fb08f7 Master_Info_File: /data/mysql/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) 2.3、测试同步 2.3.1、在MYSQL-1服务器10.11.22.78上创建一个数据库:MYSZHANG mysql> CREATE DATABASE MYSZHANG; Query OK, 1 row affected (0.00 sec) 2.3.2、在MYSQL-2服务器10.11.22.248上查看同步情况 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | MYSZHANG | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.02 sec) 2.4、测试数据库同步成功 3、安装keepalived-1.2.13 3.1、下载Keepalived安装包 http://rpmfind.net/linux/rpm2html/search.php?query=keepalived(x86-64) 3.2、安装Keepalived,用yum --nogpgcheck localinstall 在本地解决依赖关系 [root@MYSQL-1 ~]# yum -y --nogpgcheck localinstall keepalived-1.2.13-5.el6_6.x86_64.rpm 3.3、双节点配置Keepalived [root@MYSQL-1 ~]# vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 270680962@qq.com } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.11.22.55/24 dev eth0 label eth0:0 } } 3.4、测试方法如上 3.5、主备节点配置,备节点只需将 state MASTER改为state BACKUP,优先级priority 100调低 ! Configuration File for keepalived global_defs { notification_email { 270680962@qq.com } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #主节点,备节点调整为BACKUP interface eth0 virtual_router_id 51 priority 100 #备节点优先级调低于主节点即可 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.11.22.55/24 dev eth0 label eth0:0 } } virtual_server 10.11.22.55 3306 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 #persistence_timeout 50 protocol TCP real_server 10.11.22.78 3306 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 10.11.22.248 3306 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } } 3.6、测试方法如上,不论主主还是主备测试成功。主备模式down掉运行的主节点数据库服务器,备节点会接管主节 点继续运行。
标签:mysql主主 mysql主备 keepalived+mysql
原文地址:http://leamonzhang.blog.51cto.com/11334931/1766343