標籤:gbk weight ipaddress engine 查詢 with ext from toc
Mysql主主同步實現
1,基本的思路是排除資料庫單點故障,保障資料高可用性。
2,前期對於要求不高的話可以用主主同步來實現,後期如果不能滿足要求可以在後面加從資料庫。
3,實驗需要的環境及資料庫的版本如下表格:
序號 |
名稱 |
IP地址 |
資料庫版本 |
系統版本 |
1 |
MySQL-Master01 |
10.93.58.72 |
mysql-5.5.32 |
CentOS release 6.9 |
2 |
MySQL-Master02 |
10.93.58.73 |
mysql-5.5.32 |
CentOS release 6.9 |
3 |
Test |
10.93.58.70 |
無 |
CentOS release 6.9 |
4 |
virtual IP |
10.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 <<EOF
[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設定檔
[[email protected] ~]# 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:
[[email protected] ~]# mysql -uroot -p
Enter password:
輸入密碼:hwg123
4)給資料庫授權:mysql> grant replication slave on *.* to 'rep'@'10.93.58.73' identified by "hwg123";
5)查看主庫狀態;mysql> show master status;
6)在mater02上要做如下幾步操作;
7)進入資料庫;輸入密碼hwg123
[[email protected] ~]# 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;
5,切換到Master02上正式安裝mysql-5.5.32。
1)使用指令碼安裝這裡略過
2)查看設定檔,這裡注意標紅的地方:
[[email protected] ~]# 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
[[email protected] ~]# mysql -uroot -p
Enter password:
4)給資料庫授權:mysql> grant replication slave on *.* to 'rep'@'10.93.58.72' identified by "hwg123";
5)查看資料庫狀態:mysql> show master status;
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;
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設定檔
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
#####MASTER keepalived設定檔######
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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
}
}
[[email protected] ~]# vim mysql.sh vim mysql.sh
#!/bin/bash
/etc/init.d/keepalived stop
[[email protected] ~]# chmod +x mysql.sh
在Master02上操作基本和Master01類似,只需要改keepalived.conf,配置如下,注意標紅的地方:
#####SLAVE keepalived設定檔#####
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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。
停了資料庫後,看看日誌顯示;
[[email protected] ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
[[email protected] ~]# tail -fn20 /var/log/messages
隨後查看Master02上面的ip看看VIP有沒有漂移過來。
[[email protected] ~]# ip a
最後可以看出來IP已經漂移過來了,實驗成功了。
Mysql主主