mysql集群 配置Keepalived+mm

集团公司已经在oracle方向有成熟的几十套环境,但是为了节约成本,要尝试下mysql下面先用两台linux x86 Red Hat Enterprise Linux Server release 5.4 (Tikanga) 和linux6.3  安装测试下性能。

 

环境:

节点1:192.168.6.235

节点2:192.168.6.79

 

 

 【配置第一个节点235】

 

1. 解压tar包
cd /root/Downloads/
tar -xvf MySQL-5.6.15-1.el6.i686.rpm-bundle.tar

2. 以RPM方式安装MySQL
在RHEL系统中,必须先安装“MySQL-shared-compat-5.6.15-1.el6.i686.rpm”这个兼容包,然后才能安装server和client,否则安装时会出错。
yum install MySQL-shared-compat-5.6.15-1.el6.i686.rpm # RHEL兼容包
yum install MySQL-server-5.6.15-1.el6.i686.rpm # MySQL服务端程序
yum install MySQL-client-5.6.15-1.el6.i686.rpm # MySQL客户端程序
yum install MySQL-devel-5.6.15-1.el6.i686.rpm # MySQL的库和头文件
yum install MySQL-shared-5.6.15-1.el6.i686.rpm # MySQL的共享库

3. 配置MySQL登录密码
cat /root/.mysql_secret # 获取MySQL安装时生成的随机密码
service mysql start # 启动MySQL服务
mysql -uroot -p # 进入MySQL,使用之前获取的随机密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password'); # 在MySQL命令行中设置root账户的密码为password
quit # 退出MySQL命令行
service mysql restart # 重新启动MySQL服务

 

4.从新修改登录限制

[root@localhost ~]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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> use mysql;
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 host, user from user;
+-----------------------+------+
| host                  | user |
+-----------------------+------+
| 127.0.0.1             | root |
| ::1                   | root |
| localhost             | root |
| localhost.localdomain | root |
+-----------------------+------+

mysql> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' -- 这个无所谓

mysql> select host, user from user;
+-----------------------+------+
| host                  | user |
+-----------------------+------+
| %                     | root |
| 127.0.0.1             | root |
| ::1                   | root |
| localhost.localdomain | root |
+-----------------------+------+
4 rows in set (0.00 sec)

mysql> FLUSH   PRIVILEGES 
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.15    |
+-----------+
1 row in set (0.00 sec)

 

 

【配置第2个节点79】

以同样的方式配置6.79

步骤同上 (省略)

 

 

【主从配置】

先以79作为主数据库,235作从数据库

 

看看79的/etc/my.cnf

[root@localhost ~]# more /etc/my.cnf
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
lower_case_table_names=1  #忽略大小写
# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id	= 1         #第一个节点
binlog-do-db =roamingsms_v1  #主库(要同步的数据库)
replicate-do-db = roamingsms_v1 #从库 
binlog-ignore-db=mysql #忽略的数据库
binlog-ignore-db=test #忽略的数据库
log-bin=mysql-bin

 
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/var
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/var
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout


 

 

看看235的/etc/my.cnf

[root@localhost ~]# more /etc/my.cnf
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
lower_case_table_names=1   # 大小写忽略

# binary logging format - mixed recommended
binlog_format=mixed

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id	= 2   #节点2,每个节点只要不一样就行,不一定是2
replicate-do-db = roamingsms_v1 #从库(同步的数据库)
log-bin         = mysql-bin  #同步的日志

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]


 

在主库79重启mysql

#service mysqld restart                    #重启mysql

登陆Mysql

mysql> grant replication slave,reload,super on *.* to'sync'@'%' identified by 'mengliang'; #分配一个用户用于同步

mysql> flush privileges;  #权限生效

mysql> show master status;
+------------------+----------+---------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB  | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+---------------+------------------+-------------------+
| mysql-bin.000046 |      120 | roamingsms_v1 | mysql,test       |                   |
+------------------+----------+---------------+------------------+-------------------+
1 row in set (0.00 sec)

 

在从库235

mysql> flush logs;             

mysql>stop slave;                        

mysql>reset slave;

mysql>change master to master_host='192.168.6.79',master_user='sync',master_password='broadtech',master_log_file='mysql-bin.000046',master_log_pos=120;

mysql> flush privileges;

mysql> show slave status\G;                   #查看状态

 

主从配置成功。

 

 

【主从配置】

先以235作为主数据库,79作从数据库

在235

#service mysqld restart #重启mysql

登陆Mysql

mysql> grant replication slave,reload,super on *.* to'sync'@'%' identified by 'mengliang'; #分配一个用户用于同步

mysql> flush privileges; #权限生效

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 |      725 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

然后再79配置

mysql> flush logs;

mysql>stop slave;

mysql>reset slave;

mysql>change master to master_host='192.168.6.79',master_user='sync',master_password='broadtech',master_log_file='mysql-bin.000007',master_log_pos=725;

mysql> flush privileges;

mysql> show slave status\G; #查看状态

 

从主配置完成

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两项都显示Yes时说明从235同步数据成功。
至此235、79互为主从设置成功!

 

双机MM完成。

 

 

【高可用配置】

1.利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟VIP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换实现failover (下面详细实施步骤)

2.mmm模式 (以后慢慢介绍...)

3.mha模式(以后慢慢介绍...)

4.cluster模式(以后慢慢介绍...)

 

 先在79上配置

安装keepalived

# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

# yum -y install gcc gcc-c++ openssl-devel

# tar xf keepalived-1.2.2.tar.gz

# cd keepalived-1.2.2

# ./configure --prefix=/usr/local/keepalived

# make && make install

# 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/

# mkdir /etc/keepalived

 

 

 

keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换;

 

1、keepalived设置:
6.79服务器上面,编辑keeplaived.conf配置文件:
vi /usr/local/keepalived/etc/keepalived/keepalived.conf
[root@localhost keepalived]# vi /usr/local/keepalived/etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
   router_id mysql-ha
}

vrrp_instance VI_1 {
! Configuration File for keepalived

global_defs {
   router_id mysql-ha
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 79
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.6.82
    }
}

virtual_server 192.168.6.82 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 192.168.6.79 3306 {
        weight 1
        notify_down /usr/local/keepalived/etc/keepalived/mysql.sh
        TCP_CHECK {
            connect_port 3306
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
        }
    }
}


 

1.编辑mysql服务停止后的切换脚本:mysql.sh
[root@localhost /]# vi /usr/local/keepalived/etc/keepalived/mysql.sh

 

2、启动79上面的keepalived
[root@localhost /]# /usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf -D
 
 
3. ping 192.168.6.82发现已经可以ping通,并且用192.168.6.82这个IP也能够连接到数据库服务器,这里实现了235和79 虚拟成82.
 面对任意一台的死机,failover,可以透明。
 

 

先在235上配置

重复79的操作。

 

Keepalived双主高可用配置已经配置好了,可以测试。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值