[] MySQL database distributed architecture

MySQL distributed architecture

Sub-machine: sub-libraries

1. Configure SSH

2. firewall, iptables (block ports), selinux (tampering)

service iptables stop

3.mysql which a remote user needs to open

grant all privileges on *.* to 'hal'@'%'  identified by '123456' with grant option;

4. Find [mysqld] module file

Mysql from the master copy is a binary file transfer

Do separate read and write, a dispersion pressure of the database, the backup data may be real-time

The primary server: open your own account, the name of the IP address, port number, binary files, passwords to the server.

Information from the server to the primary server is recorded in the configuration item, after configuration is complete, it will go to access the main server from the server, will own the IP, port, to stay in the main server.

When operating actions of the primary server, the main server from service informs, the new service connection from the primary server, the copied binlog file, execute binlog among the statements.

When operation of the operation takes place from the server, the primary server will not be affected, from the other server is not affected

1. The process of profiling

  • 1. First find the mysql configuration file
找到[mysqld]模块
查看到数据文件的存储位置 例如/data/mysql

进入到数据存储目录中查看log日志的文件前缀

配置log-bin=日志的文件前缀
配置server-id=ip地址不加上点 (备注:不能重复)
配置的是黑洞引擎log-slave-updates=1(备注:将二进制文件同步)(级联复制)

#master必须要配置的
binlog-do-db=test  #需要同步的数据库
binlog-ignore-db=mysql #避免同步的数据库
#slave必须要配置的
replicate-do-db=test  #需要复制的数据库
replicate-ignore-db=mysql  #避免复制mysql库
  • 2. Reboot the server
service mysqld restart

systemctl stop mysqld.service
systemctl start mysqld.service
  • 3. The terminal enters the
#主服务器
show master status\G   #显示当前的主服务器状态
*************************** 1. row ***************************
             File: mysql-bin.000059        #从服务器拷贝当前的文件
         Position: 154                                #对接口令
     Binlog_Do_DB:                               #同步哪个数据中的数据,值为空复制所有的库
 Binlog_Ignore_DB:                           #避免同步那个数据中的数据
Executed_Gtid_Set: 

#从服务器
stop slave;  #停止从服务器的运行
#配置项
change master to
master_host='10.11.52.111' ,
master_user='hal',
master_password='123456',
master_log_file='mysql-bin.000022',
master_log_pos=3064;
#开启从服务
start slave;
#查看从服务器的状态
show slave status\G;
######下个两个配置项必须是yes##########
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#innodb引擎配置
###############################服务器#################
log-bin=mysql-bin
#将主服务器的log文件更新到mysql-bin中
log-slave-updates=1
server-id = 101152106
#master必须要配置的
binlog-do-db=test  #需要同步的数据库
binlog-ignore-db=mysql #避免同步的数据库
######################################################

default_storage_engine = InnoDB
#blackhole引擎配置
###############################服务器#################
log-bin=mysql-bin
#将主服务器的log文件更新到mysql-bin中
log-slave-updates=1
server-id = 101152105
#master必须要配置的
binlog-do-db=test  #需要同步的数据库
binlog-ignore-db=mysql #避免同步的数据库
#slave必须要配置的
replicate-do-db=test  #需要复制的数据库
replicate-ignore-db=mysql  #避免复制mysql库
#默认使用BLACKHOLE
default-storage-engine = BLACKHOLE
#不使用innodb
skip-innodb
######################################################

#default_storage_engine = InnoDB
#default-storage-engine = MyISAM
#myisam服务器
#从服务器引擎
log-bin=mysql-bin
#将主服务器的log文件更新到mysql-bin中
log-slave-updates=1  #级联复制的时候使用的
server-id = 101152103
#slave必须要配置的
replicate-do-db=test  #需要复制的数据库
replicate-ignore-db=mysql  #避免复制mysql库
#默认使用
default-storage-engine = myisam
#不使用innodb
skip-innodb
##################################################
#default_storage_engine = InnoDB

2. Different from the master copy

  • A master-slave

  • Mutual mainly from

Here Insert Picture Description

  • Cascading replication

Here Insert Picture Description

  • Black Hole optimized duplication

Here Insert Picture Description

From the main face questions

No fault:

  • Configuring the black hole engine, so that the black hole from the server link, thus easing pressure on the main server
  • Cascading replication, a master server from a server, the server from below as well as from a server, the server can add the following three to five servers.

3. The separate read and write

Why use separate read and write?

  • The face of enormous pressure to access the performance of a single server bottleneck, the need to share the load

Read-write mode:

  • 1. In the code to configure them:

    • Internal applications use logic judgment code is divided, as long as the code module can be built using
    • Reduce some difficulties deployment
    import pymysql
    
    class MyCat():
        def __init__(self,sql):
            self.sql = sql.strip()
    
        def query(self):
            #select * from table_name;
            head = self.sql.split(' ')[0]
            if head == 'select':
                return self.read()
            else:
                return self.write()
    
        def write(self):
            conn = pymysql.connect(host='10.11.52.106', user='hal', passwd="123456",
                             db='test', port=3306)
            cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
            try:
                cursor.execute(self.sql)
                conn.commit()
                return [{'res':'ok'}]
            except Exception as e:
                conn.rollback()
                return  '语句执行失败:\n'+str(e)
            finally:
                cursor.close()
                conn.close()
    
        def read(self):
            conn = pymysql.connect(host='10.11.52.103', user='hal', passwd="123456",
                             db='test', port=3306)
            cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
            try:
                cursor.execute(self.sql)
                conn.commit()
                result = cursor.fetchall()
                return result
            except Exception as e:
                conn.rollback()
                print('查询失败:\n'+str(e))
            finally:
                cursor.close()
                conn.close()
    
    
    sql = "insert into `qwer1` set id=2"
    action = MyCat(sql)
    print(action.query())
    
  • 2. middleware layer to achieve

Here Insert Picture Description

  • Management more convenient (mycat, java written)
  • Build a very tedious
准备jdk
准备mycat
#下载
scp root@10.11.52.106:/root/mycat.tar.gz .
scp root@10.11.52.106:/root/jdk8.tar.gz .
    
#上传
scp mycat.tar.gz root@101.123.186.200:/root/.

jdk installation configuration

#解压 
tar fx jdk8.tar.gz -C /usr/java
#进入目录
cd /usr/java
#修改名称
mv jdk1.8.0_161/  jdk1.8.0
#配置环境变量
vim /etc/profile +

##########################javaenv############################
JAVA_HOME=/usr/java/jdk1.8.0
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin/
export JAVA_HOME CLASSPATH  PATH

#刷新
source /etc/profile

Configuration and installation mycat

#解压
tar fx mycat.tar.gz -C /usr/local/
#切换目录
cd /usr/local/mycat
#主要目录介绍
conf-------->配置项
logs-------->查看mycat的运行状态

#配置环境变量
vim /etc/profile +

########################mycatenv##############################
MYCAT_HOME=/usr/local/mycat
PATH=$PATH:$MYCAT_HOME/bin/
export MYCAT_HOME PATH

#刷新
source /etc/profile

#条件
vim /etc/hosts  #本地DNS
#10.11.52.105 Centos7

Configure the user's login information

#配置mycat连接服务器
vim /usr/local/mycat/conf/server.xml
#
</system>
#配置用户名
        <user name="root">
        #密码
                <property name="password">123456</property>
        #可以访问的库
                <property name="schemas">test</property>
        </user>

        <user name="hal">
              <property name="password">123456</property>
                <property name="schemas">test</property>
       #读写权限
                <property name="readOnly">false</property>
        </user>

4. The main high availability (failover) from

Here Insert Picture Description

Virtual Redundant Routing Configuration

keepalived installation

yum install keepalived -y
#配置文件路径
vim /etc/keepalived/keepalived.conf

Configuration Item

#主机器配置
global_defs {
	#出故障了把信息发给谁,邮件
   notification_email {
      	[email protected]
   }
   #用谁的邮件发送
   notification_email_from [email protected]
   #smtp邮件服务器
   smtp_server smtp.qq.com
   #邮件如果30秒内没有发出去
   smtp_connect_timeout 30
 #昵称
   router_id lvs_1   
}
#router_id
vrrp_instance lvs_1 {
	#确定主机身份
    state MASTER
    #绑定使用的网卡,可以使用ifconfig来查看
    interface ens33
     #路由id,要求主从相同(备注:如果不相同,会裂脑)
    virtual_router_id 1
    #权重大的为主 
    priority 100
    #故障切换需要消耗的时间 ,单位是秒
    advert_int 1
    #主机的口令,两边必须一致
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    #虚拟的路由地址,在公网中的话,需要多使用一张网卡,可以多接入一个空闲的ip
    #局域网中空闲的ip很多
    virtual_ipaddress {
        10.11.52.200
        10.11.52.201
        10.11.52.202
    }
}
#从机器
global_defs {
	#出故障了把信息发给谁,邮件
   notification_email {
      	[email protected]
   }
   #用谁的邮件发送
   notification_email_from [email protected]
   #smtp邮件服务器
   smtp_server smtp.qq.com
   #邮件如果30秒内没有发出去
   smtp_connect_timeout 30
 #昵称
   router_id lvs_2   
}
#router_id
vrrp_instance lvs_2 {
	#确定主机身份
    state BACKUP
    #绑定使用的网卡,可以使用ifconfig来查看
    interface eth0
     #路由id,要求主从相同(备注:如果不相同,会裂脑)
    virtual_router_id 1
    #权重大的为主 
    priority 99
    #故障切换需要消耗的时间 ,单位是秒
    advert_int 1
    #主机的口令,两边必须一致
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    #虚拟的路由地址,在公网中的话,需要多使用一张网卡,可以多接入一个空闲的ip
    #局域网中空闲的ip很多
    virtual_ipaddress {
        10.11.52.200
        10.11.52.201
        10.11.52.202
    }
}

Start keepalived

service keepalived start

How to verify whether the configuration

ip addr #获取当前的ip   也可以获取代理ip
Published 116 original articles · won praise 10 · views 1367

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/104954439