• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • mysql主從復制,半同步復制,并行復制,讀寫分離,分布式mysql配置

    一、主從復制(常用方法)

    1、主從復制(異步復制)過程

    1.主數據庫(Master)將變更信息寫入到二進制日志文件中,這里需要注意的是舊版本的MySQL數據庫默認是不開啟二進制日志的,強烈建議在安裝好數據庫啟動之前一定要先檢查一下二進制日志文件是否開啟,即使不做主從復制架構也要開啟,否則當數據庫啟動之后再開啟二進制日志時需要重新啟動數據庫。

    2.從數據庫(Slave)開啟一個IO工作線程,通過該IO線程與主數據庫建立一個普通客戶端連接,主數據庫會啟動一個二進制日志轉儲線程(binglog dump thread),從數據庫的IO線程通過這個轉儲線程讀取主庫上的變更事件,并將變更事件記錄到中繼日志中(relay_log),如果從數據庫的IO線程讀取速度追趕上主庫的事件變更,在沒有得到新變更的通知時,IO線程會進入Sleep狀態。

    3.從數據庫還會啟動一個SQL Thread線程,這個線程從中繼日志(relay_log)中讀取變更事件,并將變更同步到從數據庫中。同時,可以通過配置選項,除了將變更存儲到數據庫中,也可以將變更事件同時存儲在從數據庫的二進制日志中

    缺點

    mysql主從復制存在的問題:

    主庫宕機后,數據可能丟失
    從庫只有一個sql Thread,主庫寫壓力大,復制很可能延時
    

    這里寫圖片描述

    2、主從復制配置

    配置環境
    master機:server1(172.25.254.1)
    slave機:server2(172.25.254.2)
    1、master機和slave機安裝mysql
    slave機同master機

    [root@server1 ~]# ls
    mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
    mysql-community-client-5.7.17-1.el6.x86_64.rpm
    mysql-community-common-5.7.17-1.el6.x86_64.rpm
    mysql-community-devel-5.7.17-1.el6.x86_64.rpm
    mysql-community-embedded-5.7.17-1.el6.x86_64.rpm
    mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
    mysql-community-libs-5.7.17-1.el6.x86_64.rpm
    mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
    mysql-community-server-5.7.17-1.el6.x86_64.rpm
    mysql-community-test-5.7.17-1.el6.x86_64.rpm
    [root@server1 ~]# yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm  mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm -y

    2、mysql初始化配置
    slave機通master機

    [root@server1 ~]# /etc/init.d/mysqld start     ##開啟mysql
    Initializing MySQL database:                               [  OK  ]
    Installing validate password plugin:                       [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [root@server1 ~]# grep password /var/log/mysqld.log    ##mysql開啟時會生成在臨時密碼,可在日志中找到
    2018-07-06T16:24:35.681054Z 1 [Note] A temporary password is generated for root@localhost: 6daOpiXZ/70r   ##臨時密碼
    2018-07-06T16:25:52.144630Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.kQ4uRR.sql' started.
    2018-07-06T16:25:52.335081Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.kQ4uRR.sql' ended.
    2018-07-06T16:25:54.406557Z 0 [Note] Shutting down plugin 'sha256_password'
    2018-07-06T16:25:54.406560Z 0 [Note] Shutting down plugin 'mysql_native_password'
    2018-07-06T16:25:56.577087Z 3 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
    [root@server1 ~]# mysql_secure_installation    ##初始化

    3、master機配置

    [root@server1 ~]# vim /etc/my.cnf
     29 server-id=1  
     30 log-bin=mysql-bin    ##開啟二進制日志
     [root@server1 ~]# /etc/init.d/mysqld restart 
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [root@server1 ~]# mysql -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.17-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> grant replication slave on *.* to wuyanzu@'172.25.254.%' identified by 'Wuyanzu+007';   ##授權
    Query OK, 0 rows affected, 1 warning (0.11 sec)
    
    mysql> flush privileges;    ##同步
    Query OK, 0 rows affected (0.10 sec)
    
    mysql> show master status;   ##查看master機狀態
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 |      603 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    mysql> 

    4、slave機配置

    [root@server2 ~]# vim /etc/my.cnf
     29 server-id=2
    [root@server2 ~]# /etc/init.d/mysqld restart 
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:   
    [root@server2 ~]# mysql -p 
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.17 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> change master to master_host='172.25.254.1',master_user='wuyanzu',master_password='Wuyanzu+007',master_log_file='mysql-bin.000001',master_log_pos=603;  ##和master機建立認證
    Query OK, 0 rows affected, 2 warnings (1.02 sec)
    
    mysql> start slave;   ##開啟slave
    Query OK, 0 rows affected (0.67 sec)
    
    mysql> show slave status\G;  ##查看
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.25.254.1
                      Master_User: wuyanzu
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 603
                   Relay_Log_File: server2-relay-bin.000002
                    Relay_Log_Pos: 320
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 603
                  Relay_Log_Space: 529
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 1
                      Master_UUID: 0e09c92a-8139-11e8-9d84-52540013d792
                 Master_Info_File: /var/lib/mysql/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)

    如果 Slave_IO_Running 是 Connecting,原因:網絡、授權、log_file/log_pos

    5、測試

    master機寫入數據:

    mysql> create database user;
    Query OK, 1 row affected (0.14 sec)
    
    mysql> use user;
    Database changed
    mysql> create table usertb;
    ERROR 1113 (42000): A table must have at least 1 column
    mysql> create table usertb (
        -> username varchar(20) not null,
        -> age varchar(4) not null);
    Query OK, 0 rows affected (1.04 sec)
    
    mysql> show tables;
    +----------------+
    | Tables_in_user |
    +----------------+
    | usertb         |
    +----------------+
    1 row in set (0.00 sec)
    mysql> insert into usertb values('wuyanzu','18');
    Query OK, 1 row affected (0.42 sec)
    
    mysql> select * from usertb;
    +----------+-----+
    | username | age |
    +----------+-----+
    | wuyanzu  | 18  |
    +----------+-----+
    1 row in set (0.00 sec)

    slave機查看:

    mysql> select * from user.usertb;
    +----------+-----+
    | username | age |
    +----------+-----+
    | wuyanzu  | 18  |
    +----------+-----+
    1 row in set (0.00 sec)

    二、GTID方式配置主從復制

    1、slave機配置

    關閉slave,修改配置文件,重新配置

    mysql> stop slave;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> ^DBye
    [root@server2 ~]# vim /etc/my.cnf
    
     29 server-id=2
     30 gtid_mode = ON
     31 enforce-gtid-consistency = true
    [root@server2 ~]# /etc/init.d/mysqld restart 
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:                                           [  OK  ]

    2、master機配置

    [root@server1 ~]# vim /etc/my.cnf
     29 server-id=1
     30 log-bin=mysql-bin
     31 gtid_mode = ON
     32 enforce-gtid-consistency = true
    [root@server1 ~]# /etc/init.d/mysqld restart 
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [root@server1 ~]# mysql -p 
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.17-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000002 |      154 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    mysql> insert into user.usertb values('chenguanxi','19');
    Query OK, 1 row affected (0.38 sec)
    
    mysql> show master status;
    +------------------+----------+--------------+------------------+----------------------------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
    +------------------+----------+--------------+------------------+----------------------------------------+
    | mysql-bin.000002 |      422 |              |                  | 0e09c92a-8139-11e8-9d84-52540013d792:1 |
    +------------------+----------+--------------+------------------+----------------------------------------+

    3、slave機做認證配置

    root@server2 ~]# mysql -p 
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6
    Server version: 5.7.17 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql> stop slave;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> change master to master_host='172.25.254.1',master_user='wuyanzu',master_password='Wuyanzu+007',master_auto_position=1;
    Quemysql> start slave;
    Query OK, 0 rows affected (0.54 sec)
    
    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.25.254.1
                      Master_User: wuyanzu
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000002
              Read_Master_Log_Pos: 422
                   Relay_Log_File: server2-relay-bin.000002
                    Relay_Log_Pos: 635
            Relay_Master_Log_File: mysql-bin.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 422
                  Relay_Log_Space: 844
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 1
                      Master_UUID: 0e09c92a-8139-11e8-9d84-52540013d792
                 Master_Info_File: /var/lib/mysql/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 0e09c92a-8139-11e8-9d84-52540013d792:1
                Executed_Gtid_Set: 0e09c92a-8139-11e8-9d84-52540013d792:1
                    Auto_Position: 1
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    ry OK, 0 rows affected, 2 warnings (0.44 sec)

    4、測試

    1、master機寫入

    mysql> insert into user.usertb values('zhoujielun','20');
    Query OK, 1 row affected (0.25 sec)
    
    mysql> select * from user.usertb;
    +------------+-----+
    | username   | age |
    +------------+-----+
    | wuyanzu    | 18  |
    | chenguanxi | 19  |
    | zhoujielun | 20  |
    +------------+-----+
    3 rows in set (0.00 sec)

    2、slave機查看

    mysql> select * from user.usertb;
    +------------+-----+
    | username   | age |
    +------------+-----+
    | wuyanzu    | 18  |
    | chenguanxi | 19  |
    | zhoujielun | 20  |
    +------------+-----+
    3 rows in set (0.00 sec)

    主從復制效果達到

    三、半同步復制

    1、原理

    這里寫圖片描述

    2、在GTID主從復制的基礎上導入半同步模塊模塊

    1、master機:

    mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';   ##導入半同步master模塊
    Query OK, 0 rows affected (0.11 sec)
    
    mysql> set global rpl_semi_sync_master_enabled=1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show variables like 'rpl%';
    +-------------------------------------------+------------+
    | Variable_name                             | Value      |
    +-------------------------------------------+------------+
    | rpl_semi_sync_master_enabled              | ON         |
    | rpl_semi_sync_master_timeout              | 10000      |
    | rpl_semi_sync_master_trace_level          | 32         |
    | rpl_semi_sync_master_wait_for_slave_count | 1          |
    | rpl_semi_sync_master_wait_no_slave        | ON         |
    | rpl_semi_sync_master_wait_point           | AFTER_SYNC |
    | rpl_stop_slave_timeout                    | 31536000   |
    +-------------------------------------------+------------+
    7 rows in set (0.00 sec)
    mysql> show status like 'rpl%';
    +--------------------------------------------+-------+
    | Variable_name                              | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients               | 0     |
    | Rpl_semi_sync_master_net_avg_wait_time     | 0     |
    | Rpl_semi_sync_master_net_wait_time         | 0     |
    | Rpl_semi_sync_master_net_waits             | 0     |
    | Rpl_semi_sync_master_no_times              | 0     |
    | Rpl_semi_sync_master_no_tx                 | 0     |
    | Rpl_semi_sync_master_status                | ON    |
    | Rpl_semi_sync_master_timefunc_failures     | 0     |
    | Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
    | Rpl_semi_sync_master_tx_wait_time          | 0     |
    | Rpl_semi_sync_master_tx_waits              | 0     |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
    | Rpl_semi_sync_master_wait_sessions         | 0     |
    | Rpl_semi_sync_master_yes_tx                | 0     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)

    2、slave機配置

    mysql> select * from mysql.gtid_executed;
    +--------------------------------------+----------------+--------------+
    | source_uuid                          | interval_start | interval_end |
    +--------------------------------------+----------------+--------------+
    | 0e09c92a-8139-11e8-9d84-52540013d792 |              1 |            1 |
    | 0e09c92a-8139-11e8-9d84-52540013d792 |              2 |            2 |
    +--------------------------------------+----------------+--------------+
    2 rows in set (0.00 sec)
    
    mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
    Query OK, 0 rows affected (0.11 sec)
    
    mysql> show variables like 'rpl%';
    +---------------------------------+----------+
    | Variable_name                   | Value    |
    +---------------------------------+----------+
    | rpl_semi_sync_slave_enabled     | OFF      |
    | rpl_semi_sync_slave_trace_level | 32       |
    | rpl_stop_slave_timeout          | 31536000 |
    +---------------------------------+----------+
    3 rows in set (0.02 sec)
    
    mysql> set global rpl_semi_sync_slave_enabled=1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show variables like 'rpl%';
    +---------------------------------+----------+
    | Variable_name                   | Value    |
    +---------------------------------+----------+
    | rpl_semi_sync_slave_enabled     | ON       |
    | rpl_semi_sync_slave_trace_level | 32       |
    | rpl_stop_slave_timeout          | 31536000 |
    +---------------------------------+----------+
    3 rows in set (0.01 sec)
    
    mysql> show status like 'rpl%';
    +----------------------------+-------+
    | Variable_name              | Value |
    +----------------------------+-------+
    | Rpl_semi_sync_slave_status | OFF   |
    +----------------------------+-------+
    1 row in set (0.00 sec)
    
    mysql> stop slave io_thread;    ##重新開啟io線程,使它完全打開
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> start slave io_thread;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show status like 'rpl%';
    +----------------------------+-------+
    | Variable_name              | Value |
    +----------------------------+-------+
    | Rpl_semi_sync_slave_status | ON    |
    +----------------------------+-------+
    1 row in set (0.00 sec)

    3、測試

    master機寫入數據,并接受到slave機返回的ack值

    mysql> insert into user.usertb values('liudehua','21');
    Query OK, 1 row affected (0.26 sec)
    
    mysql> show status like 'rpl%';
    +--------------------------------------------+-------+
    | Variable_name                              | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients               | 1     |
    | Rpl_semi_sync_master_net_avg_wait_time     | 0     |
    | Rpl_semi_sync_master_net_wait_time         | 0     |
    | Rpl_semi_sync_master_net_waits             | 1     |
    | Rpl_semi_sync_master_no_times              | 0     |
    | Rpl_semi_sync_master_no_tx                 | 0     |
    | Rpl_semi_sync_master_status                | ON    |
    | Rpl_semi_sync_master_timefunc_failures     | 0     |
    | Rpl_semi_sync_master_tx_avg_wait_time      | 1372  |
    | Rpl_semi_sync_master_tx_wait_time          | 1372  |
    | Rpl_semi_sync_master_tx_waits              | 1     |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
    | Rpl_semi_sync_master_wait_sessions         | 0     |
    | Rpl_semi_sync_master_yes_tx                | 1     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)

    將slave機的io線程停掉,master機寫入數據,會默認等待10s,如果超時還未等到slave機的ack,將自動切換到異步復制,如果slave機的io線程再次開啟,復制將自動切換到半同步方式
    slave機

    mysql> stop slave io_thread;
    Query OK, 0 rows affected (0.04 sec)

    master機

    mysql> insert into user.usertb values('zhangxueyou','22');
    Query OK, 1 row affected (10.45 sec)
    
    mysql> show status like 'rpl%';
    +--------------------------------------------+-------+
    | Variable_name                              | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients               | 0     |
    | Rpl_semi_sync_master_net_avg_wait_time     | 0     |
    | Rpl_semi_sync_master_net_wait_time         | 0     |
    | Rpl_semi_sync_master_net_waits             | 1     |
    | Rpl_semi_sync_master_no_times              | 1     |
    | Rpl_semi_sync_master_no_tx                 | 1     |
    | Rpl_semi_sync_master_status                | OFF   |
    | Rpl_semi_sync_master_timefunc_failures     | 0     |
    | Rpl_semi_sync_master_tx_avg_wait_time      | 1372  |
    | Rpl_semi_sync_master_tx_wait_time          | 1372  |
    | Rpl_semi_sync_master_tx_waits              | 1     |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
    | Rpl_semi_sync_master_wait_sessions         | 0     |
    | Rpl_semi_sync_master_yes_tx                | 1     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)

    四、并行復制

    1、查看從庫的sql類型

    mysql> show processlist;
    +----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
    | Id | User        | Host      | db   | Command | Time  | State                                                  | Info             |
    +----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
    |  1 | system user |           | NULL | Connect | 52393 | Slave has read all relay log; waiting for more updates | NULL             |
    |  2 | system user |           | NULL | Connect |   415 | Waiting for master to send event                       | NULL             |
    |  7 | root        | localhost | NULL | Query   |     0 | starting                                               | show processlist |
    +----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
    3 rows in set (0.00 sec)

    2、slave機配置

    [root@server2 ~]# vim /etc/my.cnf
    slave-parallel-type=LOGICAL_CLOCK
    slave-parallel-workers=16   ##線程數
    master_info_repository=TABLE    ##master_info存儲方式為:table
    relay_log_info_repository=TABLE ##relay_log_info存儲方式為:table
    relay_log_recovery=ON
    [root@server2 ~]# /etc/init.d/mysqld restart 
    Stopping mysqld:                                           [  OK  ]
    Starting mysqld:                                           [  OK  ]

    3、登陸從庫查看sql類型

    mysql> show processlist;
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    | Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    |  1 | system user |           | NULL | Connect |   86 | Slave has read all relay log; waiting for more updates | NULL             |
    |  2 | system user |           | NULL | Connect |   87 | Waiting for master to send event                       | NULL             |
    |  4 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    |  5 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    |  6 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    |  7 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    |  8 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    |  9 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 10 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 11 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 13 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 15 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 16 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 17 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 18 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 19 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 20 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 21 | system user |           | NULL | Connect |   87 | Waiting for an event from Coordinator                  | NULL             |
    | 22 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    19 rows in set (0.00 sec)

    查看mysql的表,新增了下面兩個表

    mysql> use mysql;
    mysql> show tables;
    ----------------------------
     slave_master_info         
     slave_relay_log_info 
    ----------------------------

    五、mysql-proxy讀寫分離配置

    配置環境
    master機:server1(172.25.254.1)
    slave機:server2(172.25.254.2)
    調度器:server3(172.25.254.3)

    1、調度器配置

    1、安裝讀寫分離代理mysql-proxy

    [root@server3 ~]# ls
    mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
    [root@server3 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
    [root@server3 ~]# cd /usr/local/
    [root@server3 local]# ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
    [root@server3 local]# ll
    total 44
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 bin
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 etc
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 games
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 include
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 lib
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 lib64
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 libexec
    lrwxrwxrwx  1 root root    38 Jul  7 16:56 mysql-proxy -> mysql-proxy-0.8.5-linux-el6-x86-64bit/
    drwxr-xr-x  8 7161 wheel 4096 Aug 19  2014 mysql-proxy-0.8.5-linux-el6-x86-64bit
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 sbin
    drwxr-xr-x. 5 root root  4096 Jun 19 23:38 share
    drwxr-xr-x. 2 root root  4096 Jun 28  2011 src

    2、為了測試讀寫分離效果,修改lua腳本

    [root@server3 local]# cd mysql-proxy/share/doc/mysql-proxy/
    [root@server3 mysql-proxy]# vim rw-splitting.lua 
     38 if not proxy.global.config.rwsplit then
     39         proxy.global.config.rwsplit = {
     40                 min_idle_connections = 1,
     41                 max_idle_connections = 2,  ##表示連接數超過兩個就開始讀寫分離
     42 
     43                 is_debug = false
     44         }
     45 end

    3、編寫配置文件

    [root@server3 mysql-proxy]# pwd
    /usr/local/mysql-proxy
    [root@server3 mysql-proxy]# mkdir logs
    [root@server3 mysql-proxy]# mkdir conf
    [root@server3 mysql-proxy]# vim conf/mysql-proxy.conf
      1 [mysql-proxy]
      2 user=root
      3 proxy-address=172.25.254.3:3306
      4 proxy-read-only-backend-addresses=172.25.254.2:3306
      5 proxy-backend-addresses=172.25.254.1:3306
      6 proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.l    ua
      7 log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
      8 log-level=debug
      9 daemon=true
     10 keepalive=true

    3、啟動mysql-proxy

    [root@server3 mysql-proxy]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf  ##第一次啟動失敗,原因是因為配置文件權限過大
    2018-07-07 17:13:41: (critical) mysql-proxy-cli.c:326: loading config from '/usr/local/mysql-proxy/conf/mysql-proxy.conf' failed: permissions of /usr/local/mysql-proxy/conf/mysql-proxy.conf aren't secure (0660 or stricter required)
    2018-07-07 17:13:41: (message) Initiating shutdown, requested from mysql-proxy-cli.c:328
    2018-07-07 17:13:41: (message) shutting down normally, exit code is: 1
    [root@server3 mysql-proxy]# chmod 550 conf/mysql-proxy.conf 
    [root@server3 mysql-proxy]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf   ##再次啟動成功

    測試:
    1、master機授權用戶可遠程登陸:

    mysql> grant select,insert,update on user.* to wuyanzu@'172.25.254.%' identified by 'Wuyanzu+007';
    Query OK, 0 rows affected, 1 warning (0.36 sec)

    2、master機和slave機安裝lsof,以便查看效果
    3、物理機拿三個shell測試

    [root@foundation77 ~]# mysql -h 172.25.254.3 -u wuyanzu -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 5.7.17-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MySQL [(none)]> 

    master機查看

    [root@server1 ~]# lsof -i :3306
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    mysqld  1168 mysql   34u  IPv6   8487      0t0  TCP *:mysql (LISTEN)
    mysqld  1168 mysql   52u  IPv6   9264      0t0  TCP server1:mysql->server3:51667 (ESTABLISHED)
    mysqld  1168 mysql   53u  IPv6   9165      0t0  TCP server1:mysql->server2:57130 (ESTABLISHED)
    mysqld  1168 mysql   55u  IPv6   9265      0t0  TCP server1:mysql->server3:51668 (ESTABLISHED)

    slave機查看

    [root@server2 ~]# lsof -i :3306
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    mysqld  1705 mysql   32u  IPv6   9424      0t0  TCP *:mysql (LISTEN)
    mysqld  1705 mysql   48u  IPv6   9577      0t0  TCP server2:mysql->server3:60675 (ESTABLISHED)
    mysqld  1705 mysql   50u  IPv4   9438      0t0  TCP server2:57130->server1:mysql (ESTABLISHED)

    我們在連接slave機的客戶端上寫入數據,看是否寫入到master機,如果寫入,表示讀寫分離成功
    連接slave機客戶端寫入

    MySQL [user]> show tables;
    +----------------+
    | Tables_in_user |
    +----------------+
    | usertb         |
    +----------------+
    1 row in set (0.00 sec)
    
    MySQL [user]> insert into user.usertn values('huge','23');
    ERROR 1146 (42S02): Table 'user.usertn' doesn't exist
    MySQL [user]> insert into user.usertb values('huge','23');
    Query OK, 1 row affected (0.38 sec)

    master機查看

    mysql> select * from user.usertb;
    +-------------+-----+
    | username    | age |
    +-------------+-----+
    | wuyanzu     | 18  |
    | chenguanxi  | 19  |
    | zhoujielun  | 20  |
    | liudehua    | 21  |
    | zhangxueyou | 22  |
    | huge        | 23  |
    +-------------+-----+
    6 rows in set (0.00 sec)

    寫入成功,讀寫分離效果實現

    六、分布式mysql配置

    1、server1配置

    1、配置文件修改

    [root@server1 ~]# vim /etc/my.cnf
     29 server_id=1
     30 gtid_mode=ON
     31 enforce_gtid_consistency=ON
     32 master_info_repository=TABLE
     33 relay_log_info_repository=TABLE
     34 binlog_checksum=NONE
     35 log_slave_updates=ON
     36 log_bin=binlog
     37 binlog_format=ROW
     38 
     39 transaction_write_set_extraction=XXHASH64
     40 loose-group_replication_group_name="6884089d-81cd-11e8-b10c-52540013d792"    ##uuid  可以在數據庫中用select uuid();查看
     41 loose-group_replication_start_on_boot=off
     42 loose-group_replication_local_address= "172.25.254.1:24901"
     43 loose-group_replication_group_seeds= "172.25.254.1:24901,172.25.254.2:24901,    172.25.254.3:24901"
     44 loose-group_replication_bootstrap_group=off
     45 
     46 loose-group_replication_ip_whitelist='127.0.0.1/8,172.25.254.0/24'
     47 loose-group_replication_enforce_update_everywhere_checks=true
     48 loose-group_replication_single_primary_mode=false

    2、重置mysql

    [root@server1 mysql]# rm -fr *
    [root@server1 mysql]# /etc/init.d/mysqld start 
    Initializing MySQL database:                               [  OK  ]
    Installing validate password plugin:                       [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [root@server1 mysql]# pwd
    /var/lib/mysql
    [root@server1 mysql]# grep password /var/log/mysqld.log   ##查找臨時密碼

    3、master機配置

    [root@server1 mysql]# mysql -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.17-log
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> set sql_log_bin=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> alter user root@localhost identified by 'Wuyanzu+007';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> grant replication slave on *.* to wuyanzu@'%' identified by 'Wuyanzu+007';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set sql_log_bin=1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> change master to master_user='wuyanzu',master_password='Wuyanzu+007' for channe
    Query OK, 0 rows affected, 2 warnings (0.65 sec)
    
    mysql> install plugin group_replication soname 'group_replication.so';
    Query OK, 0 rows affected (0.19 sec)
    
    mysql> set global group_replication_bootstrap_group=on;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> start group_replication;
    Query OK, 0 rows affected (1.59 sec)
    
    mysql> set global group_replication_bootstrap_group=off;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from performance_schema.replication_group_members;
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | group_replication_applier | 57b54493-81cf-11e8-8a9a-52540013d792 | server1     |        3306 | ONLINE       |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    1 row in set (0.00 sec)

    2、server2配置

    1、配置文件

     29 server_id=2
     30 gtid_mode=ON
     31 enforce_gtid_consistency=ON
     32 master_info_repository=TABLE
     33 relay_log_info_repository=TABLE
     34 binlog_checksum=NONE
     35 log_slave_updates=ON
     36 log_bin=binlog
     37 binlog_format=ROW
     38 
     39 transaction_write_set_extraction=XXHASH64
     40 loose-group_replication_group_name="6884089d-81cd-11e8-b10c-52540013d792"
     41 loose-group_replication_start_on_boot=off
     42 loose-group_replication_local_address= "172.25.254.2:24901"
     43 loose-group_replication_group_seeds= "172.25.254.1:24901,172.25.254.2:24901,172.25.254.3    :24901"
     44 loose-group_replication_bootstrap_group=off
     45 
     46 loose-group_replication_ip_whitelist='127.0.0.1/8,172.25.254.0/24'
     47 loose-group_replication_enforce_update_everywhere_checks=true
     48 loose-group_replication_single_primary_mode=false

    2、初始化

    [root@server2 ~]# /etc/init.d/mysqld stop 
    Stopping mysqld:                                           [  OK  ]
    [root@server2 ~]# vim /etc/my.cnf
    [root@server2 ~]# cd /var/lib/mysql
    [root@server2 mysql]# ls
    auto.cnf         ib_buffer_pool  performance_schema        server2-relay-bin.000010  user
    ca-key.pem       ibdata1         private_key.pem           server2-relay-bin.index
    ca.pem           ib_logfile0     public_key.pem            server-cert.pem
    client-cert.pem  ib_logfile1     server2-relay-bin.000008  server-key.pem
    client-key.pem   mysql           server2-relay-bin.000009  sys
    [root@server2 mysql]# rm -fr *

    3、mysql腳本文件修改,注釋掉密碼插件
    /etc/init.d/mysqld

    111             ret=$?
    112             #[ $ret -ne 0 ] && return $ret
    113             #initfile="$(install_validate_password_sql_file)"
    114             #action $"Installing validate password plugin: " /usr/sbin/mysqld --datadir=    "$datadir" --user=mysql --init-file="$initfile"
    115             #ret=$?
    116             #rm -f "$initfile"
    117             chown -R mysql:mysql "$datadir"

    4、開啟mysql并配置

    [root@server2 mysql]# mysql -p 
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.17-log
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> set sql_log_bin=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> alter user root@localhost identified by 'Wuyanzu+007';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> grant replication slave on *.* to wuyanzu@'%' identified by 'Wuyanzu+007';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set sql_log_bin=1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>  change master to master_user='wuyanzu',master_password='Wuyanzu+007' for channel 'group_replication_recovery';
    Query OK, 0 rows affected, 2 warnings (0.89 sec)
    
    mysql> install plugin group_replication soname 'group_replication.so';
    Query OK, 0 rows affected (0.28 sec)
    
    mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> start group_replication;
    Query OK, 0 rows affected (7.37 sec)
    
    mysql> select * from performance_schema.replication_group_members;
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | group_replication_applier | 57b54493-81cf-11e8-8a9a-52540013d792 | server1     |        3306 | ONLINE       |
    | group_replication_applier | c33132d0-81d1-11e8-9ff1-525400677cc8 | server2     |        3306 | RECOVERING   |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    2 rows in set (0.00 sec)
    ###首次查看狀態RECOVERING,有可能是在同步數據,可以等會再查看,
    mysql> select * from performance_schema.replication_group_members;
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | group_replication_applier | 57b54493-81cf-11e8-8a9a-52540013d792 | server1     |        3306 | ONLINE       |
    | group_replication_applier | c33132d0-81d1-11e8-9ff1-525400677cc8 | server2     |        3306 | ONLINE       |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    2 rows in set (0.00 sec)

    3、server3配置同server2

    [root@server3 mysql]# /etc/init.d/mysqld start 
    Initializing MySQL database:                               [  OK  ]
    Starting mysqld:                                           [  OK  ]
    [root@server3 mysql]# grep password /var/log/mysqld.log 
    2018-07-07T10:57:25.628882Z 1 [Note] A temporary password is generated for root@localhost: p&,!dOLNA83p
    2018-07-07T10:58:12.128260Z 3 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
    [root@server3 mysql]# mysql -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.17-log
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> set sql_log_bin=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> alter user root@localhost identified by 'Wuyanzu+007';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> grant replication slave on *.* to wuyanzu@'%' identified by 'Wuyanzu+007';
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set sql_log_bin=1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> change master to master_user='wuyanzu',master_password='Wuyanzu+007' for channel 'group_replication_recovery';
    Query OK, 0 rows affected, 2 warnings (1.38 sec)
    
    mysql> install plugin group_replication soname 'group_replication.so';
    Query OK, 0 rows affected (0.20 sec)
    
    mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> start group_replication;
    Query OK, 0 rows affected (4.31 sec)
    
    mysql> select * from performance_schema.replication_group_members;
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | group_replication_applier | 57b54493-81cf-11e8-8a9a-52540013d792 | server1     |        3306 | ONLINE       |
    | group_replication_applier | 88460791-81d4-11e8-a6e6-525400e9cc87 | server3     |        3306 | RECOVERING   |
    | group_replication_applier | c33132d0-81d1-11e8-9ff1-525400677cc8 | server2     |        3306 | ONLINE       |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    3 rows in set (0.00 sec)
    
    mysql> select * from performance_schema.replication_group_members;
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    | group_replication_applier | 57b54493-81cf-11e8-8a9a-52540013d792 | server1     |        3306 | ONLINE       |
    | group_replication_applier | 88460791-81d4-11e8-a6e6-525400e9cc87 | server3     |        3306 | ONLINE       |
    | group_replication_applier | c33132d0-81d1-11e8-9ff1-525400677cc8 | server2     |        3306 | ONLINE       |
    +---------------------------+--------------------------------------+-------------+-------------+--------------+
    3 rows in set (0.00 sec)
    

    4、測試:

    server3寫入

    mysql> create database test;
    Query OK, 1 row affected (0.26 sec)
    
    mysql> use test;
    Database changed
    mysql> create table t1 (c1 int primary key,c2 text not null);   ##插入表時需要設置主鍵
    Query OK, 0 rows affected (1.11 sec)
    
    mysql> insert into t1 values(1,'wuyanzu');
    Query OK, 1 row affected (0.24 sec)

    server2查看

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    | westos             |
    +--------------------+
    6 rows in set (0.00 sec)
    
    mysql> use test;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | t1             |
    +----------------+
    1 row in set (0.00 sec)
    
    mysql> select * from t1;
    +----+---------+
    | c1 | c2      |
    +----+---------+
    |  1 | wuyanzu |
    +----+---------+
    1 row in set (0.00 sec)
    版權聲明:本文為weixin_41789003原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/weixin_41789003/article/details/80947395

    智能推薦

    MySQL主從復制(Master-Slave),半同步復制與讀寫分離(MySQL-Proxy)

    Mysql作為目前世界上使用最廣泛的免費數據庫,在實際的生產環境中,由單臺Mysql作為獨立的數據庫是完全不能滿足實際需求的,無論是在安全性,高可用性以及高并發等各個方面。 因此,一般來說都是通過 主從復制(Master-Slave)的方式來同步數據,再通過讀寫分離(MySQL-Proxy)來提升數據庫的并發負載能力 這樣的方案來進行部署與實施的。 如下圖所示: 復制架構衍生史 MySQL的復制架...

    mysql半同步復制與并行復制

    ####################################相關概念################################################### (1)異步復制:主庫上的事務不會等待從庫的確認即返回客戶端提交成功! (2)同步復制:主庫上提交的事務向客戶端返回成功之前,需要收到所有從庫提交事務的確認信息。 (3)半同步復制:異步復制和同步復制的折中,主庫上提交...

    mysql主從復制 半同步復制

    當單臺MYSQL服務器無法滿足當前網站流量時的優化方案。需要搭建mysql集群技術。 復制方式:(同步方式) 主–從復制 主–主復制 半同步復制 復制原理: Mysql中有一種日志叫做bin日志(二進制日志)。這個日志會記錄下所有修改了數據庫的SQL語句(insert,update,delete,ALTER TABLE,grant等等)。 主從復制的原理其實就是把主服務器上...

    mysql 主從復制優化、并行復制

    實驗環境: server2 為 server3 的 master server3 為 server1 的 master 即server1、2、3的身份如下: server2:master server3:master / slave server2:slave 由于先前做過其他數據庫實驗,環境是server2為master, server1和server3為slave 實驗操作: server1、...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    猜你喜歡

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    requests實現全自動PPT模板

    http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...

    Linux C系統編程-線程互斥鎖(四)

    互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...

    精品国产乱码久久久久久蜜桃不卡