linux下高可用mysql

 

    MySQL主從雙向複製(腳本實現)
 
 
1主服務器的主機名爲:master,從服務器的主機名爲:slave
2安裝MYSQL包
# rpm –ivh mysql-5.0.77-4.2.1.AXS3.i386.rpm
3在主庫啓動MYSQL
#service mysql start
4在主庫編輯/etc/my.cnf,
#vim /etc/my.cnf
[mysqld]配置段添加如下字段
server-id=1
log-bin=cjxylog
編輯好my.cnf後重啓mysql
#service mysqld restart
5主庫增加用戶,用於從庫讀取主庫日誌
>grant replication slave on *.* to 'slave'@'10.10.10.52' identified by 'slave';
啓動後用show master status查看主庫的日誌文件
#mysql
>show master status
6啓動從庫的mysql
#service mysqld start
 
7修改從庫/etc/my.cnf,增加選項
#Mysql
#vim /etc/my.cnf
添加如下內容
 
server-id=2
master-host=10.10.10.51
master-user=slave
master-password=slave
master-port=3306
master-connect-retry=60
編輯完後重啓mysql
#service mysqld restart
 
 
8重啓後用show命令查看從庫slave的運行情況
#mysql
>stop slave;
>start slave;
>show slave status\G;
 
 
 
9測試
 
主庫創建名爲cjxy的數據庫並用show databases;命令查看
#mysql
>start master
>create database cjxy;
>show databases;
 
 
 
 
 
 
 
 
從庫只須用show命令查看即可
#mysql
>show databases;
 
 
10單向複製成功,編寫腳本,實現主從雙向複製和故障自動切換角色 ,從而實現MYSQL數據庫高可用性.
在兩臺MYSQL服務器上配置3份MYSQL服務配置文件
分別爲:
/etc/my.cnf
/etc/my.cnf.master
/etc/my.cnf.slave
其中,原主庫服務器(IP地址:10.10.10.51)配置文件內容如下:
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=1
log-bin=cjxylog
 
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
 
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
/etc/my.cnf.master
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=1
log-bin=cjxylog
 
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
 
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
/etc/my.cnf.slave
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
master-host=10.10.10.52
master-user=slave
master-password=slave
master-port=3306
master-connect-retry=60
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
原從庫服務器(IP地址:10.10.10.52)配置文件內容如下:
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
master-host=10.10.10.51
master-user=slave
master-password=slave
master-port=3306
master-connect-retry=60
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
/etc/my.cnf.master
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=1
log-bin=swaplog
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
/etc/my.cnf.slave
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
master-host=10.10.10.51
master-user=slave
master-password=slave
master-port=3306
master-connect-retry=60
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
服務器主從自動切換,當數據庫服務器有一臺離線,另外一臺可以上線開始工作,實現高可用。
首先在原主庫服務器(IP地址:10.10.10.51)上寫入腳本,當主庫服務器與WEB服務器失去聯繫後自動變成從屬服務器(如果此服務器沒有宕機)
腳本內容如下:
#/bin/bash
#原MYSQL 主服務器使用的腳本
check=`cat /etc/my.cnf | grep server-id`
if [ "$check" = "server-id=1" ]
then
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
if [ "$link" != "10.10.10.100:" ]
then
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
if [ "$link" = "10.10.10.52:" ]
then
Ifdown eth0:200
cat /etc/my.cnf.slave > /etc/my.cnf
service mysqld restart
fi
else
fi
else
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.52 | grep "from" | awk '{print $4}'`
if [ "$link" != "10.10.10.52:" ]
then
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
if [ "$link" = "10.10.10.100:" ]
then
ifconfig eth0:200 10.10.10.200 netmask 255.255.255.0
cat /etc/my.cnf.master > /etc/my.cnf
service mysqld restart
fi
else
fi
fi
然後在從服務器(IP地址:10.10.10.52)寫入腳本,當從庫與主庫失去聯繫後,從庫自動變成主庫
腳本內容如下:
#/bin/bash
#原MYSQL從服務器使用的腳本
check=`cat /etc/my.cnf | grep server-id`
if [ "$check" != "server-id=1" ]
then
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
 
if [ "$link" != "10.10.10.51:" ]
then
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
if [ "$link" = "10.10.10.100:" ]
ifconfig eth0:200 10.10.10.200 netmask 255.255.255.0
cat /etc/my.cnf.master > /etc/my.cnf
service mysqld restart
fi
else
fi
 
else
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.100 | grep "from" | awk '{print $4}'`
if [ "$link" != "10.10.10.100:" ]
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
link=`ping -c 1 10.10.10.51 | grep "from" | awk '{print $4}'`
if [ "$link" = "10.10.10.51:" ]
then
Ifdown eth0:200
cat /etc/my.cnf.slave > /etc/my.cnf
service mysqld restart
fi
else
fi
fi
將以上腳本分別命名並放置在/opt/mysqlchange.sh
並且賦予執行權限 chmod a+x /opt/mysqlchange.sh
然後啓動循環任務計劃,每一分鐘執行一次該腳本
Crontab –e
*/1 * * * * bash /opt/mysqlchange.sh
:
一些錯誤信息的處理,主從服務器上的命令,及狀態信息。
在從服務器上使用show slave status\G
Slave_IO_Running, 爲No,
則說明IO_THREAD沒有啓動,請執行start slave io_thread
 
Slave_SQL_Running爲 No
1.首先停掉Slave服務:slave stop
2.到主服務器上查看主機狀態:
記錄File和Position對應的值。
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000020 | 135617781 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
3.到slave服務器上執行手動同步:
mysql> change master to
> master_host='master_ip',
> master_user='user',
> master_password='pwd',
> master_port=3307,
> master_log_file='mysql-bin.000020',
> master_log_pos=135617781;
1 row in set (0.00 sec)
> slave start;
1 row in set (0.00 sec)
再次查看slave狀態發現:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
..
Seconds_Behind_Master: 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章