溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

數(shù)據(jù)庫(kù)---mysql主從復(fù)制讀寫(xiě)分離

發(fā)布時(shí)間:2020-06-06 08:07:15 來(lái)源:網(wǎng)絡(luò) 閱讀:502 作者:linux_lp 欄目:數(shù)據(jù)庫(kù)

http://m.open-open.com/m/lib/view/1413274853450.html 原理及架構(gòu)分析

部署前準(zhǔn)備

下載好源碼包存放位置要與腳本中對(duì)應(yīng)  

mysql-5.5.22.tar.gz,cmake-2.8.6.tar.gz,amoeba-mysql-binary-2.2.0.tar.gz,jdk-6u14-linux-x64.bin

selinux和iptables不做設(shè)置,關(guān)閉

系統(tǒng)光盤(pán)鏡像為本地yum源,配置好yum文件

環(huán)境介紹:

服務(wù)器(master):192.168.100.155

從服務(wù)器(slave1,slave2):192.168.100.153-154

代理服務(wù)器(amoeba):192.168.100.156

應(yīng)用客戶(hù)端(app):192.168.100.157


1.搭建時(shí)間服務(wù)器:

主節(jié)點(diǎn)上安裝ntp時(shí)間服務(wù):192.168.100.155

yum -y install ntp

sed -i '/^server/s/^/#/g' /etc/ntp.conf 

cat <<END >>/etc/ntp.conf

server 127.127.1.0

fudge 127.127.1.0 stratum 8

END

/etc/init.d/ntpd restart

netstat -utpln |grep ntp

從節(jié)點(diǎn)同步時(shí)間:192.168.100.153-154

yum -y install ntpdate

/usr/sbin/ntpdate 192.168.100.155


2.安裝mysql:192.168.100.153-155

提示:

[root@localhost ~]# cd bin/

[root@localhost bin]# vi mysql_install.sh 

#!/bin/bash

##第一配置yum,安裝ncurses依賴(lài)包

yum -y install ncurses-*

#解壓cmake,安裝基礎(chǔ)環(huán)境

tar zxvf /root/cmake-2.8.6.tar.gz -C /usr/src/

cd /usr/src/cmake-2.8.6

#配置,編譯安裝cmake

./configure &&gmake &&gmake install

##解壓mysql

tar zxvf /root/mysql-5.5.22.tar.gz -C /usr/src/

cd /usr/src/mysql-5.5.22/

#cmake進(jìn)行配置mysql

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql   #指定安裝目錄\

-DDEFAULT_CHARSET=utf8   #指定字符集為utf8 \

-DDEFAULT_COLLATION=utf8_general_ci   ##指定字符校驗(yàn) \

-DWITH_EXTRA_CHARSETS=all   ##支持額外字符集\

-DSYSCONFDIR=/etc/  ##指定配置文件位置

make &&make install   #編譯安裝

if [ -e /usr/local/mysql ];then

echo "mysql install successfully."

fi

:wq

[root@localhost bin]# vi mysql_config.sh 

#!/bin/bash

#1.復(fù)制配置文件

cp /usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf

#2.添加系統(tǒng)服務(wù)

cp /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld  on

#3.優(yōu)化PATH路徑,執(zhí)行命令時(shí)方便,單引號(hào)雙引號(hào)都行

grep mysql /etc/profile

if [ $? -eq 0 ];then

echo "PATH is set."

else

echo "export PATH=$PATH:/usr/local/mysql/bin"  >>/etc/profile

source /etc/profile  ##執(zhí)行文件

fi

#4.初始化mysql,創(chuàng)建用戶(hù),賦權(quán)

useradd -M -s /sbin/nologin mysql

chown -R mysql:mysql /usr/local/mysql

/usr/local/mysql/scripts/mysql_install_db  \

--basedir=/usr/local/mysql \

--datadir=/usr/local/mysql/data --user=mysql

#5.啟動(dòng)mysql,并設(shè)置為開(kāi)機(jī)啟動(dòng)

if [ -e /tmp/mysql.sock ];then

/etc/init.d/mysqld restart

else

/etc/init.d/mysqld start

fi

chkconfig mysqld on

#6.修改密碼,并提示密碼

mysqladmin -u root password '123123'  &&echo "mysql root password is 123123"

:wq

3.配置MySQL主從復(fù)制:

1)主服務(wù)器配置:192.168.100.155

sed -i 's/^log-bin=.*/log-bin=master-bin\nlog-slave-updates=ture/g' /etc/my.cnf

sed -i '/^server-id/s/1/11/g' /etc/my.cnf 

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123123';

mysql> flush privileges;

mysql> show master status;  ##記住File的及Position的值,此處為master-bin.000001和337

mysql> create database db_test;  ##創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)

mysql> quit

2)配置從服務(wù)器1:192.168.100.153

sed -i '/^server-id/s/1/22/g' /etc/my.cnf

sed -i '/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index' /etc/my.cnf

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> change master to master_host='192.168.100.155',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=337;

mysql> start slave;

mysql> show slave status\G;  ##查看無(wú)error即可

mysql> show databases;  ##驗(yàn)證數(shù)據(jù)庫(kù)是否同步

mysql> quit

3)配置從服務(wù)器2:192.168.100.154

sed -i '/^server-id/s/1/33/g' /etc/my.cnf

sed -i '/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index' /etc/my.cnf

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> change master to master_host='192.168.100.155',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=337;

mysql> start slave;

mysql> show slave status\G;  ##查看無(wú)error即可

mysql> show databases;  ##驗(yàn)證數(shù)據(jù)庫(kù)是否同步

mysql> quit


4.搭建MySQL讀寫(xiě)分離:

1)安裝軟件:192.168.100.156

yum -y remove java

chmod +x jdk-6u14-linux-x64.bin 

./jdk-6u14-linux-x64.bin 

mv jdk1.6.0_14/ /usr/local/jdk1.6

vi /etc/profile

export JAVA_HOME=/usr/local/jdk1.6

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin

export AMOEBA_HOME=/usr/local/amoeba

export PATH=$PATH:$AMOEBA_HOME

:wq

source /etc/profile

java -version

mkdir /usr/local/amoeba

tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/

chmod -R 755 /usr/local/amoeba/

/usr/local/amoeba/bin/amoeba  ##驗(yàn)證

2)數(shù)據(jù)授權(quán)給amoeba讀寫(xiě)權(quán)限:

mysql> grant all on *.* to linuxfan@'192.168.100.%' identified by '123123';   ##在192.168.100.155上完成

mysql> show grants for linuxfan@'192.168.100.%';   ##在192.168.100.153-154上查看是否同步了權(quán)限

3)修改配置文件:192.168.100.156

vim /usr/local/amoeba/conf/amoeba.xml

 30                                         <property name="user">amoeba</property>

 31 

 32                                         <property name="password">123456</property>

115                 <property name="defaultPool">master</property>

116 

117                 <property name="writePool">master</property>   ##注意刪除<!--  -->的注釋

118                 <property name="readPool">slaves</property>

:set nu  ##顯示行號(hào)

:wq


vim /usr/local/amoeba/conf/dbServers.xml

 25                         <!-- mysql user -->

 26                         <property name="user">linuxfan</property>   ##該用戶(hù)必須是上一步授權(quán)的用戶(hù)

 27                         

 28                         <!--  mysql password -->   ##修改

 29                         <property name="password">123123</property>  ##刪除下一行的“-->”

 44         <dbServer name="master"  parent="abstractServer">   ##修改為master

 45                 <factoryConfig>

 46                         <!-- mysql ip -->

 47                         <property name="ipAddress">192.168.100.155</property>  ##指定正確的master的ip

 51         <dbServer name="slave1"  parent="abstractServer">  ##修改為slave1

 52                 <factoryConfig>

 53                         <!-- mysql ip -->

 54                         <property name="ipAddress">192.168.100.153</property>  ##指定slave1的ip地址

 55                 </factoryConfig>

 56         </dbServer>

 57         <dbServer name="slave2"  parent="abstractServer">  ##添加如下6行,指定slave2的ip

 58                 <factoryConfig>

 59                         <!-- mysql ip -->

 60                         <property name="ipAddress">192.168.100.154</property>

 61                 </factoryConfig>

 62         </dbServer>

 64         <dbServer name="slaves" virtual="true">  ##修改為slaves

 70                         <property name="poolNames">slave1,slave2</property>  ##修改集群的成員名稱(chēng)用逗號(hào)隔開(kāi)

:wq

/usr/local/amoeba/bin/amoeba start&   ##啟動(dòng)代理服務(wù)

netstat -utpln |grep 8066  ##驗(yàn)證



5.測(cè)試讀寫(xiě)分離

1)驗(yàn)證主從復(fù)制:192.168.100.157

yum -y install mysql

mysql -uamoeba -p123456 -h 192.168.100.156 -P 8066   ##登錄db集群192.168.100.157

mysql>show databases;

mysql> use db_test;

mysql> create table linuxfan(id int(10),name varchar(10),address varchar(20));

在192.168.100.153-155上查看結(jié)果:

mysql -uroot -p123123 

mysql> use db_test;

mysql> show tables;   ##已然同步


2)關(guān)閉slave1,slave2的復(fù)制功能:192.168.100.153-154

mysql> stop slave;


3)分別在master,slave1,slave2上創(chuàng)建不同的數(shù)據(jù):

master:

mysql> insert into linuxfan values(1,'hehe','this is master');

slave1:

mysql> insert into linuxfan values(2,'hehe','this is slave1');

slave2:

mysql> insert into linuxfan values(3,'hehe','this is slave2');


4)應(yīng)用客戶(hù)端驗(yàn)證讀:192.168.100.157

mysql> select * from linuxfan;  ##第一次查詢(xún)

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    2 | hehe | this is slave1 |

+------+------+----------------+

1 row in set (0.02 sec)


mysql> select * from linuxfan;   ##第二次查詢(xún)

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    3 | hehe | this is slave2 |

+------+------+----------------+

1 row in set (0.01 sec)

mysql> select * from linuxfan;   ##第三次查詢(xún)

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    2 | hehe | this is slave1 |

+------+------+----------------+

1 row in set (0.00 sec)


5)應(yīng)用客戶(hù)端上驗(yàn)證寫(xiě):

mysql> insert into linuxfan values(4,'hehe','app write test');  ##寫(xiě)入數(shù)據(jù)

Query OK, 1 row affected (0.02 sec)


mysql> select * from linuxfan;   ##查不到剛寫(xiě)入的數(shù)據(jù)

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    3 | hehe | this is slave2 |

+------+------+----------------+

1 row in set (0.01 sec)


master上驗(yàn)證:

mysql> select * from linuxfan;   ##查到數(shù)據(jù)

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    1 | hehe | this is master |

|    4 | hehe | app write test |

+------+------+----------------+

2 rows in set (0.00 sec)


總結(jié):

app寫(xiě)入數(shù)據(jù)時(shí),amoeba會(huì)將數(shù)據(jù)路由到master上進(jìn)行存儲(chǔ),app讀取數(shù)據(jù)時(shí),amoeba會(huì)將讀的請(qǐng)求以輪詢(xún)的方式發(fā)給slaves組(slave1+slave2),實(shí)現(xiàn)讀寫(xiě)分離。

master和slaves間配置了主從復(fù)制,保證了數(shù)據(jù)的一致性。

主從復(fù)制實(shí)現(xiàn)高可用   讀寫(xiě)分離實(shí)現(xiàn)高性能

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI