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

A brief introduction to mysql master-slave replication, read-write separation + tomcat projects, etc.

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Load Balancer (Server Load Balancing) is a network service device that distributes traffic to multiple Cloud Virtual Machine (computing clusters). It can quickly improve the external service capability of the application system through traffic distribution; hide the actual service port to enhance the security of the internal system; and improve the reliability of the application system by eliminating the single point of failure of the service. Today, I will introduce mysql master-slave copy, read-write separation +tomcat project, etc.

Nginx Load Balancer +mysql master-slave replication, read-write separation +tomcat project

Architecture: (only two virtual machines due to limited resources)

1 192.168.137.3 Nginx, Mysql (main), Tomcat1, Tomcat2

2 192.168.137.5 Mysql (from)

Version: Centos 7

192.168.137.3

1, Install Nginx first, convenient for later use

2. Install mysql

file /usr/share/mysql/charsets/swe7.xml from install of MySQL-server-5.6.13-1.linux_glibc2.5.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64

yum remove mariadb-libs

[root@localhost local]# rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm

In preparation... ################################# [100%]

Upgrading/installing...

1:MySQL-server-5.6.13-1.linux_glibc################################# [100%]

Can't locate Data/Dumper.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/mysql_install_db line 42.

BEGIN failed--compilation aborted at /usr/bin/mysql_install_db line 42.

[root@localhost local]# yum install 'perl(Data::Dumper)'

[root@localhost local]# rpm -e MySQL-server

[root@localhost local]# rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm

In preparation... ################################# [100%]

Upgrading/installing...

1:MySQL-server-5.6.13-1.linux_glibc################################# [100%]

Remember to initialize!!! is really drunk

[root@localhost ~]# mysql_install_db

[root@localhost ~]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf

[root@localhost ~]# service mysql start

Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

[root@localhost ~]# vi /etc/my.cnf and add log-error = /var/log/mysql.log to the configuration file;log file generated at initialization

[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql/

[root@localhost ~]# service mysql start

Starting MySQL. SUCCESS!

/usr/bin/mysqladmin -u root password '123123'

3. Install tomcat

install the JDK

Unzip into directory

JAVA_HOME=/usr/local/jdk1.7.0_09

CLASSPATH=$JAVA_HOME/lib/

PATH=$PATH:$JAVA_HOME/bin

export PATH JAVA_HOME CLASSPATH

[root@localhost jdk1.7.0_09]# vi /etc/profile

[root@localhost jdk1.7.0_09]# source /etc/profile

[root@localhost jdk1.7.0_09]# java -version

java version "1.7.0_09"

Java(TM) SE Runtime Environment (build 1.7.0_09-b05)

Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

Install tomcat

Unzip into directory

[root@localhost local]# mv apache-tomcat-6.0.44 tomcat

[root@localhost local]# vi /etc/profile

export TOMCAT_HOME=/usr/local/tomcat

Install tomcat2

Unzip into directory

[root@localhost local]# mv apache-tomcat-6.0.44 tomcat2

[root@localhost local]# vi /etc/profile

export TOMCAT_HOME2=/usr/local/tomcat2

Modify the corresponding port

Like 8081.

Close firewalld client testing

192.168.137.5 Install mysql (same as above)

4, configure mysql read and write separation, first set the master and slave

Master mysql:

#GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'cp'@'%' IDENTIFIED BY '123123' WITH GRANT OPTION;

Note: The above line cannot be used!!! When configuring read-write separation, the operation database permissions are not enough, use the following statement!!!!

grant all privileges on *.* to 'cp'@'%' identified by '123123' WITH GRANT OPTION;

From MySQL:

edit the configuration file

server-id = 222

relay-log = /data/relaylogs/relay-bin

mysql> CHANGE MASTER TO MASTER_HOST='192.168.137.3',MASTER_PORT=3306,MASTER_USER='cp',MASTER_PASSWORD='123123',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=2186726;

ERROR 29 (HY000): File '/data/relaylogs/relay-bin.index' not found (Errcode: 13 - Permission denied)

The requested URL/data/relaylogs/relay-bin.index was not found on this server.

After the above command is successful, start slave

Then use: show slave status\G to see if it is correct

Problem 1: When changing an existing library, mysql reports Slave_SQL_Running: No error

Solution:

mysql> reset slave ;

mysql> stop slave ;

mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

mysql> start slave ;

Problem 2: The database that already exists on master cannot be synchronized to slave.

Solution:

Reconfiguration Master and Slave!!!!!!!!!!!!!!!

Reconfigure master-slave--follow above

1. Enter the main library and lock the table to prevent data from being written.

mysql> flush tables with read lock;

2. On the slave database, directly backup the remote database to the local directory

mysqldump -h 192.168.137.3 -ucp -p123123 --databases erpBack >erpBack.bak.sql

3. Stop slave on the slave library

mysql> stop slave ;

4. Create a new erpBack library and import data

mysql> create database erpBack ;

mysql> use erpBack ;

mysql> source /root/erpBack.bak.sql

5. On the main library, unlock the table; view the binary log information; and record

mysql> unlock tables ;

mysql> show master status;

6. Reconfigure the connection information on the slave library

CHANGE MASTER TO MASTER_HOST='192.168.137.3',MASTER_PORT=3306,MASTER_USER='cp',MASTER_PASSWORD='123123',MASTER_LOG_FILE='mysql-bin.000006',MASTER_LOG_POS=120;

7. Start slave; view master-slave status

mysql> start slave ;

mysql> show slave status\G

8. Update the erpBack database data on the master database; and check whether it runs normally on the slave database!!

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Problem 3: After restarting the system the next day, show slave status\G displays an error:

Slave_IO_Running: No

Slave_SQL_Running: No

Start start slave and display error:

ERROR 29 (HY000): File '/data/relaylogs/relay-bin.index' not found (Errcode: 13 - Permission denied)

Solution: show master status; Reconfigure master-slave

mysql> reset slave ; reset slave and reconfigure slave

mysql> CHANGE MASTER TO MASTER_HOST='192.168.137.3',MASTER_PORT=3306,MASTER_USER='cp',MASTER_PASSWORD='123123',MASTER_LOG_FILE='mysql-bin.000009',MASTER_LOG_POS=467;

mysql> start slave ;

mysql> show slave status\G Check if it is correct

Configure mysql read/write separation, using amoeba

https://sourceforge.net/projects/amoeba/files/

Download the latest version; and extract

unzip amoeba-mysql-3.0.4-BETA-distribution.zip

mv amoeba-mysql-3.0.4-BETA amoeba

cd amoeba

chmod +x bin/

vi conf/amoeba.xml

#Set client connection parameters: port, username, password and other information

root

root

#Set master-slave write and read; the following contents of the original configuration file are commented out, and need to be enabled here.

server1

server2

vi conf/dbServers.xml

#Set the parameters for connecting the master and slave databases here: port, database, username, password

3306

erpBack

cp

123123

eg: Note here that both master and slave mysql need to enable remote access, and the user name and password set are the same!!

#Set the IP address of the master and slave; note: the IP address of the local machine is also replaced

192.168.137.3

192.168.137.5

Start amoeba

bin/launcher &

View logs

tail -f logs/net.log

Client-side testing:

mysql -h 192.168.137.3 -P8066 -uroot -proot

Configuring Nginx Proxy

#servers

upstream test.com {

ip_hash;

server 127.0.0.1:8080;

server 127.0.0.1:8081;

}

#The above is the hash result allocation of each request according to the access ip, so that each visitor can access a backend Cloud Virtual Machine to solve the session problem.

proxy_pass http://test.com;

proxy_redirect default;

Finally, test on the client side!

If there is anything else you need to know, you can find our professional technical engineers on the official website. Technical engineers have more than ten years of experience in the industry, so they will be more detailed and professional than Xiaobian's answers. Official website link www.yisu.com

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

Wechat

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

12
Report