mysql 1227_Mysql主主

Mysql主主同步实现

1,基本的思路是排除数据库单点故障,保障数据高可用性。

2,前期对于要求不高的话可以用主主同步来实现,后期如果不能满足要求可以在后面加从数据库。

3,实验需要的环境及数据库的版本如下表格:序号名称IP地址数据库版本系统版本

1MySQL-Master0110.93.58.72mysql-5.5.32CentOS release 6.9

2MySQL-Master0210.93.58.73mysql-5.5.32CentOS release 6.9

3Test10.93.58.70无CentOS release 6.9

4virtual IP10.93.58.74无无

4,正式安装数据步骤:

1)我是用了一个脚本来安装这个Mysql-5.5.32数据库,脚本如下:#!/bin/bash

#auto_install_mysql

#auth by tony date 2018-07-31

yum -y install gcc gcc-c++ make ncurses ncurses-devel libaio-devel cmake

groupadd mysql

useradd mysql -s/sbin/nologin -M -g mysql

mkdir /application

wget http://10.93.58.70/lamp/mysql-5.5.32.tar.gz

tar xvf mysql-5.5.32.tar.gz

cd mysql-5.5.32

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \

-DMYSQL_DATADIR=/data/mysql \

-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \

-DENABLED_LOCAL_INFILE=ON \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \

-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \

-DWITH_FAST_MUTEXES=1 \

-DWITH_ZLIB=bundled \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_READLINE=1 \

-DWITH_EMBEDDED_SERVER=1 \

-DWITH_DEBUG=0

if [ $? -eq 0 ];then

make && make install

ln -s /application/mysql-5.5.32/ /application/mysql

/bin/cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chown mysql.mysql /application/mysql

chown mysql.mysql /data

echo 'export PATH=/application/mysql/bin:$PATH '>>/etc/profile

source /etc/profile

mv /etc/my.cnf /etc/my.cnf.bak

cat >/etc/my.cnf <

[mysqld]

port            = 3306

socket          = /application/mysql-5.5.32/tmp/mysql.sock

datadir         =/data/mysql

user            =mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=mysql-bin

server-id = 1

auto_increment_offset=1

auto_increment_increment=2

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

replicate-do-db =all

EOF

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/mysql --user=mysql

/etc/init.d/mysqld start

chkconfig mysqld on

mysqladmin -uroot password 'hwg123'

echo -e "\n\033[32m-----------------------------------------------\033[0m"

echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"

else

echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"

exit 0

fi

"mysqlset.sh" 66L, 2228C written

2)查看my.cnf配置文件

[root@Mysql-Master ~]# cat /etc/my.cnf[mysqld]

port            = 3306

socket          = /application/mysql-5.5.32/tmp/mysql.sock

datadir         =/data/mysql

user            =mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=mysql-bin

binlog_format = mixed

server-id = 1

auto_increment_offset=1

auto_increment_increment=2

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

replicate-do-db =all

3)进入数据库Master01:

[root@Mysql-Master ~]# mysql -uroot  -p

Enter password:

输入密码:hwg123

4)给数据库授权:mysql>  grant replication slave on *.* to 'rep'@'10.93.58.73' identified by "hwg123";

1d96f200e46350b2a3fa57a57fea80a7.png

5)查看主库状态;mysql> show master status;

344e650555a984f68d50d0bfc4f8d80b.png

6)在mater02上要做如下几步操作;

7)进入数据库;输入密码hwg123

[root@Mysql-Slave ~]# mysql -uroot -p

Enter password:

8)根据mysql-bind和post点来授权同步库和post点位。mysql> change master to master_host='10.93.58.73',master_user='rep',master_password='hwg123',master_log_file='mysql-bin.000014',master_log_pos=263;

Query OK, 0 rows affected (0.06 sec)

9)开启slave同步

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

10)查看同步结果如果是两个YES;证明同步成功:

mysql> show slave status\G;

4456d6a1be33e240f3e0391bed66bf4b.png

5,切换到Master02上正式安装mysql-5.5.32。

1)使用脚本安装这里略过

2)查看配置文件,这里注意标红的地方:

[root@Mysql-Slave ~]# cat /etc/my.cnf[mysqld]

port            = 3306

socket          = /application/mysql-5.5.32/tmp/mysql.sock

datadir         =/data/mysql

user            =mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=mysql-bin

binlog_format = mixed

server-id = 2

auto_increment_offset=2

auto_increment_increment=2

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

replicate-do-db =all

3)进入数据库:输入密码:hwg123

[root@Mysql-Slave ~]# mysql -uroot -p

Enter password:

4)给数据库授权:mysql> grant replication slave on *.* to 'rep'@'10.93.58.72' identified by "hwg123";

50c30a5327f7754eb40a61f7d26d1795.png

5)查看数据库状态:mysql> show master status;

77cbcf998f93872a0754641a33dfd3e3.png

6)切换到master01上授权;mysql> change master to master_host='10.93.58.73',master_user='rep',master_password='hwg123',master_log_file='mysql-bin.000006',master_log_pos=263;

7)开启slave同步mysql> slave start;

8)查询同步状态:mysql> show slave status\G;

2e0dfda9d0bf9d885508ac904d7db278.png

7)这样主主同步就好了

6,安装keepalived来做高可用。我用的是keepalived-1.2.20.tar.gz

在Master01上操作如下:yum –y install openssl openssl-devel

tar xf keepalived-1.2.20.tar.gz

cd keepalived-1.2.20

./configure --with-kernel-dir=/usr/src/kernels/2.6.32-696.el6.x86_64

make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/sbin/keepalived /usr/sbin/

mkdir /etc/keepalived

cd /etc/keepalived/

添加keepalived配置文件

[root@Mysql-Master ~]# vim /etc/keepalived/keepalived.conf#####MASTER keepalived配置文件######

! Configuration File for keepalived

global_defs {

notification_email {

hwg1227@163.com

}

notification_email_from hwg1227@163.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

# VIP1

vrrp_instance VI_1 {

state MASTER

interface eth0

lvs_sync_daemon_inteface eth0

virtual_router_id 50

priority 50

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.93.58.74

}

}

virtual_server 10.93.58.74 3306 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 60

protocol TCP

real_server 10.93.58.72 3306 {

weight 1

notify_down /root/mysql.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

[root@Mysql-Master ~]#  vim  mysql.sh#!/bin/bash

/etc/init.d/keepalived stop

[root@Mysql-Master ~]# chmod +x mysql.sh

在Master02上操作基本和Master01类似,只需要改keepalived.conf,配置如下,注意SLAVE 、priority、IP几个地方:#####SLAVE keepalived配置文件#####

! Configuration File for keepalived

global_defs {

notification_email {

hwg1227@163.com

}

notification_email_from hwg1227@163.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

# VIP1

vrrp_instance VI_1 {

state SLAVE

interface eth0

lvs_sync_daemon_inteface eth0

virtual_router_id 50

priority 30

advert_int 5

#    nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.93.58.74

}

}

virtual_server 10.93.58.74 3306 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 60

protocol TCP

real_server 10.93.58.73 3306 {

weight 1

notify_down /root/mysql.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

7,最后测试一下,停掉Master01主库;没有停主库前的ip。

9542e7cab74c9bc7d072be9b06a1a301.png

停了数据库后,看看日志显示;

[root@Mysql-Master ~]# /etc/init.d/mysqld stop

Shutting down MySQL. SUCCESS!

[root@Mysql-Slave ~]# tail -fn20 /var/log/messages

c1386289157c2c17bdddcc874ea18806.png

随后查看Master02上面的ip看看VIP有没有漂移过来。

[root@Mysql-Slave ~]# ip a

4ccd6ff0189781de8a6f6057745f28c5.png

最后可以看出来IP已经漂移过来了,实验成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值