mysql------Master-slave replication, read-write separation

1. Why do master-slave replication (the role of master-slave replication)

Do hot backup of data, as a backup database, after the primary database server fails, it can switch to the secondary database to continue working to avoid data loss.
Schema extensions. The volume of business is increasing, and the I/O access frequency is too high for a single machine to satisfy. At this time, multi-library storage is used to reduce the evaluation rate of disk I/O access and improve the I/O performance of a single machine.
Read-write separation enables the database to support greater concurrency. Especially important in reports. Because the sql statements of some reports are very slow, the table is locked and the foreground service is affected. If the master is used in the foreground and the slave is used in the report, then the report sql will not cause the foreground lock, which ensures the speed of the foreground.

 1--The query work can be performed on the slave server (that is, the read function we often say), reducing the pressure on the master server; (master library write, slave library read, step-down) 2-- Backup from the master server to avoid backup
 period Affect the main server service; (to ensure data security)
 3--When the main server has a problem, it can be switched to the slave server. (improves performance)

 2. What is master-slave replication?

Master-slave replication is used to create a database environment exactly the same as the master database, called a slave database. During assignment, one server acts as the master and the other server acts as the slave.
When a slave server connects to the master server, the slave server notifies the master server to read the position of the last successful update from the server's log file. The slave then receives any updates that have occurred since then, and then locks and waits until the master is notified of a new update.

3. The principle of master-slave replication

Step 1: The update events (update, insert, delete) of the main library db are written to the binlog (binary log) Step 2: Initiate a
connection from the library to the main library
Step 3: At this time, the main library creates a binlog dump thread thread, Send the contents of the binlog to the slave library
Step 4: After the slave library is started, create an I/O thread to read the binlog content from the master library and write it to the relay log (relay log). Step 5: Also
create A SQL thread reads the content from the relay log, executes the read update event from the Exec_Master_Log_Pos position, and writes the update content to the slave's db. 

4 mysql master-slave construction

 Construction steps: Prepare two machines (the docker image of mysql simulates two machines)

    - Master library: 8.130.125.9 33307 121.196.246.157 33307
  - Slave library: 8.130.125.9 33306 121.196.246.157 33306

 Step 1: Pull the image of mysql5.7

docker pull mysql:5.7

 Step 2: Create folders, files (directory mapping)


mkdir /home/mysql
mkdir /home/mysql/conf.d
mkdir /home/mysql/data/       #主库存放数据的
touch /home/mysql/my.cnf      #主库配置文件
        
mkdir /home/mysql1
mkdir /home/mysql1/conf.d
mkdir /home/mysql1/data/     #主库存放数据的
touch /home/mysql1/my.cnf    #主库配置文件

 The third step (important): write mysql configuration file (master, slave)

 #### Main configuration ####

[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000

server-id=100           #server-id自己写,不能重复    id号唯一的
log-bin=mysql-bin       #开启binlog日志   将sql语句写进去,然后给从库

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

 Configure from library

[mysqld]
user=mysql    #mysqld用mysql用户跑
character-set-server=utf8    #编码是   utf8
default_authentication_plugin=mysql_native_password        #密码加密方式
secure_file_priv=/var/lib/mysql    #文件存放位置
expire_logs_days=7    #日志存放时间      sql严格模式
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000     #最多的连接数


server-id=101            #server-id自己写,不能重复    id号唯一的
log-bin=mysql-slave-bin      #开启binlog日志   和主库优点不一样
relay_log=edu-mysql-relay-bin    #开启中继日志

[client]
default-character-set=utf8      #client指定utf8

[mysql]
default-character-set=utf8       #mysql指定utf8

 Step 4: Start the mysql container and do port and directory mapping

 Start the main library 

docker run  -di -v /home/mysql/data/:/var/lib/mysql
 -v /home/mysql/conf.d:/etc/mysql/conf.d
 -v /home/mysql/my.cnf:/etc/mysql/my.cnf -p 33307:3306 
--name mysql-master -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

 Start slave library

docker run  -di -v /home/mysql1/data/:/var/lib/mysql
 -v /home/mysql1/conf.d:/etc/mysql/conf.d 
-v /home/mysql1/my.cnf:/etc/mysql/my.cnf -p 33306:3306
 --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

 The fifth step is to connect to the main library with cmd

mysql -uroot -P33307 -h 121.196.246.157 -p      #ip地址
#在主库创建用户并授权
##创建test用户
create user 'test'@'%' identified by '123';
##授权用户
grant all privileges on *.* to 'test'@'%' ;    #将所有的权限赋予给test用户
###刷新权限
flush privileges;
#查看主服务器状态(显示如下图)
show master status; 

Step 5: Connect from the library

mysql -uroot -P33306 -h 121.196.246.157 -p
    #配置详解
    '''
    change master to 
    master_host='MySQL主服务器IP地址', 
    master_user='之前在MySQL主服务器上面创建的用户名', 
    master_password='之前创建的密码', 
    master_log_file='MySQL主服务器状态中的二进制文件名', 
    master_log_pos='MySQL主服务器状态中的position值';
    '''
    change master to master_host='121.196.246.157',
master_port=33307,master_user='test',
master_password='123',master_log_file='mysql-bin.000003',master_log_pos=0;
    #启用从库
    start slave;
    #查看从库状态(如下图)
    show slave status\G;

  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes

 These two parameters are yes, which means that the master-slave build is successful

Step 6: Create a library in the main library, create a table, insert data, and see the slave library

 5.django achieves read-write separation

# 第一步:配置文件配置多数据库
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'db1': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db1.sqlite3',
    }
}

# 第二步:手动读写分离
Book.objects.using('db1').create(name='西游记')

# 第三步,自动读写分离
写一个py文件,db_router.py,写一个类:
class DBRouter(object):
    def db_for_read(self, model, **hints):
        # 多个从库 ['db1','db2','db3']
        return 'db1'

    def db_for_write(self, model, **hints):


        return 'default'
    
    
# 第三步:配置文件配置
DATABASE_ROUTERS = ['mysql_master_demo.db_router.DBRouter', ]

# 以后自动读写分离
# 多从库负载
# 分库分表

 

DATABASES = {
     # 主库
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'zhu',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '121.196.246.157',
        'PORT': 33307,
    },
    # 从库
    'db1': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'zhu',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '121.196.246.157',
        'PORT': 33306,
    },
}

Guess you like

Origin blog.csdn.net/xiaolisolovely/article/details/132430307