Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database-mysql master-slave replication read-write separation

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Analysis of the principle and Architecture of http://m.open-open.com/m/lib/view/1413274853450.html

Pre-deployment preparation

The location of downloading the source package should correspond to the location in the script.

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 and iptables do not set up, close

The system CD image is the local yum source, and the yum file is configured.

Environment introduction:

Master server (master): 192.168.100.155

Slave server (slave1,slave2): 192.168.100.153-154

Proxy server (amoeba): 192.168.100.156

Application client (app): 192.168.100.157

1. Set up a time server:

Install the ntp time service on the primary node: 192.168.100.155

Yum-y install ntp

Sed-I'/ ^ server/s/ ^ / # / G' / etc/ntp.conf

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

Slave node synchronization time: 192.168.100.153-154

Yum-y install ntpdate

/ usr/sbin/ntpdate 192.168.100.155

two。 Install mysql:192.168.100.153-155

Tip:

[root@localhost ~] # cd bin/

[root@localhost bin] # vi mysql_install.sh

#! / bin/bash

# # first configure yum and install ncurses dependency package

Yum-y install ncurses-*

# decompress cmake and install the basic environment

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

Cd / usr/src/cmake-2.8.6

# configure, compile and install cmake

. / configure & & gmake & & gmake install

# # decompressing mysql

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

Cd / usr/src/mysql-5.5.22/

# cmake to configure mysql

Cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql # specify the installation directory\

-DDEFAULT_CHARSET=utf8 # specifies the character set as utf8\

-DDEFAULT_COLLATION=utf8_general_ci # # specify character check\

-DWITH_EXTRA_CHARSETS=all # # support additional character sets\

-DSYSCONFDIR=/etc/ # # specify the configuration file location

Make & & make install # compilation and installation

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

Echo "mysql install successfully."

Fi

: wq

[root@localhost bin] # vi mysql_config.sh

#! / bin/bash

# 1. Copy Profil

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

# 2. Add system services

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. Optimize the PATH path to facilitate command execution, both single quotation marks and double quotation marks

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 # # execution file

Fi

# 4. Initialize mysql, create users, and empower

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. Start mysql and set it to boot

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

/ etc/init.d/mysqld restart

Else

/ etc/init.d/mysqld start

Fi

Chkconfig mysqld on

# 6. Change the password and prompt for the password

Mysqladmin-u root password '123123' & & echo mysql root password is 123123

: wq

3. Configure MySQL master-slave replication:

1) Master server configuration: 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; # # remember the values of File and Position, here are master-bin.000001 and 337

Mysql > create database db_test; # # create a test database

Mysql > quit

2) configure slave server 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; # # View without error

Mysql > show databases; # # verify that the database is synchronized

Mysql > quit

3) configure slave server 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; # # View without error

Mysql > show databases; # # verify that the database is synchronized

Mysql > quit

4. Set up MySQL read-write separation:

1) install the software: 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 # # Verification

2) data is granted read and write permissions to amoeba:

Mysql > grant all on *. * to linuxfan@'192.168.100.%' identified by '123123; # # completed on 192.168.100.155

Mysql > show grants for linuxfan@'192.168.100.%'; # # check whether permissions are synchronized on 192.168.100.153-154,

3) modify the configuration file: 192.168.100.156

Vim / usr/local/amoeba/conf/amoeba.xml

30 amoeba

thirty-one

32 123456

115 master

one hundred and sixteen

117 master # # Note deleted comments

118 slaves

: set nu # # displays the line number

: wq

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

twenty-five

26 linuxfan # # this user must be the user authorized in the previous step

twenty-seven

28 # # modify

29 123123 # # delete "-->" on the next line

44 # # modify to master

forty-five

forty-six

47 192.168.100.155 # # specify the correct ip for master

51 # # modified to slave1

fifty-two

fifty-three

54 192.168.100.153 # # specify the ip address of slave1

fifty-five

fifty-six

57 # # add the following 6 lines to specify the ip of slave2

fifty-eight

fifty-nine

60 192.168.100.154

sixty-one

sixty-two

64 # # modify to slaves

70 slave1,slave2 # # modify cluster member names separated by commas

: wq

/ usr/local/amoeba/bin/amoeba start& # # start the proxy service

Netstat-utpln | grep 8066 # # Verification

5. Test read-write separation

1) verify master-slave replication: 192.168.100.157

Yum-y install mysql

Mysql-uamoeba-p123456-h 192.168.100.156-P 8066 # Log in to db cluster 192.168.100.157

Mysql > show databases

Mysql > use db_test

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

View the results on 192.168.100.153-155:

Mysql-uroot-p123123

Mysql > use db_test

Mysql > show tables; # # has been synchronized

2) disable the replication function of slave1,slave2: 192.168.100.153-154

Mysql > stop slave

3) create different data on master,slave1,slave2:

Master:

Mysql > insert into linuxfan values (1) the is master' of this is master' is the same as that of others.

Slave1:

Mysql > insert into linuxfan values. (2) this is slave1' is the same as the last one.

Slave2:

Mysql > insert into linuxfan values. (3) this is slave2' is the same as the last one.

4) Application client verification read: 192.168.100.157

Mysql > select * from linuxfan; # # first query

+-+

| | id | name | address | |

+-+

| | 2 | hehe | this is slave1 |

+-+

1 row in set (0.02 sec)

Mysql > select * from linuxfan; # # second query

+-+

| | id | name | address | |

+-+

| | 3 | hehe | this is slave2 |

+-+

1 row in set (0.01 sec)

Mysql > select * from linuxfan; # # third query

+-+

| | id | name | address | |

+-+

| | 2 | hehe | this is slave1 |

+-+

1 row in set (0.00 sec)

5) verify the write on the application client:

Mysql > insert into linuxfan values; # # write data

Query OK, 1 row affected (0.02 sec)

Mysql > select * from linuxfan; # # cannot find the data just written

+-+

| | id | name | address | |

+-+

| | 3 | hehe | this is slave2 |

+-+

1 row in set (0.01 sec)

Verify on master:

Mysql > select * from linuxfan; # # find data

+-+

| | id | name | address | |

+-+

| | 1 | hehe | this is master |

| | 4 | hehe | app write test |

+-+

2 rows in set (0.00 sec)

Summary:

When app writes data, amoeba will route the data to master for storage. When app reads data, amoeba will send the read request to the slaves group (slave1+slave2) in a polling way to achieve read-write separation.

Master-slave replication is configured between master and slaves to ensure the consistency of data.

Master-slave replication to achieve high-availability read-write separation to achieve high performance

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report