<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          什么?你們項(xiàng)目沒用過主從復(fù)制和讀寫分離?

          共 12628字,需瀏覽 26分鐘

           ·

          2021-05-02 07:00

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

            作者 |  Dkwestworld

          來源 |  urlify.cn/meEFF3

          前言

          提示:隨著項(xiàng)目數(shù)據(jù)量的增大,我們不得不開始考慮主從復(fù)制和讀寫分離。

          一、主從復(fù)制搭建

          1、Master庫搭建(主庫搭建和從庫搭建一毛一樣)

          這里我只準(zhǔn)備了一臺服務(wù)器進(jìn)行搭建測試,遂主庫和從庫均在一臺服務(wù)器上,只不過是訪問端口不一樣而已

          第一步:檢查用戶組

          cat /etc/group | grep mysql
          cat /etc/passwd |grep mysql
          #沒有任何輸出,說明沒有則創(chuàng)建該用戶組
          #若存在mysql用戶組,可以執(zhí)行刪除指令
          userdel mysql
          #創(chuàng)建用戶組
          groupadd mysql
          useradd -r -g mysql mysql

          第二步:下載mysql包

          這邊提供了永久網(wǎng)盤資源,失效評論區(qū)找我,我會第一時(shí)間提供。
          我們下面的安裝均使用該版本進(jìn)行安裝。

          鏈接:https://pan.baidu.com/s/1nlCjsIPmKH3PQhU8g9MVGw
          提取碼:3mcg


          使用xftp軟件上傳至服務(wù)器

          #解壓
          tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

          第三步:創(chuàng)建存儲數(shù)據(jù)目錄并賦權(quán)

          mkdir /usr/local/mysql/data
          chown -R mysql:mysql /usr/local/mysql
          chmod -R 755 /usr/local/mysql

          目錄切換到/home/mysql下,所有解壓文件移動到usr/local/mysql下面

          mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

          第四步 編輯my.cnf,做一些簡單配置

          basedir=/usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64
          socket=/var/lib/mysql/mysql.sock

          # Disabling symbolic-links is recommended to prevent assorted security risks
          symbolic-links=0

          log-error=/var/log/mysqld.log
          pid-file=/var/run/mysqld/mysqld.pid
          #跳過密碼驗(yàn)證
          skip-grant-tables
          server-id=2
          log-bin=mysql-bin

          [mysqld]
          datadir=/usr/local/mysql/data
          port = 3306
          sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
          symbolic-links=0
          max_connections=400
          innodb_file_per_table=1
          #表名大小寫不明感,敏感為
          lower_case_table_names=1

          第五步啟動服務(wù)

          /usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64/support-files/mysql.server start

          #執(zhí)行該命令,可能會報(bào)如下錯誤,大多是由于權(quán)限問題,找到mysql的文件進(jìn)行賦權(quán)即可(不報(bào)錯直接命令行登錄測試,然后跳到第六步)

          [root@VM-0-10-centos mysql]# /usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64/support-files/mysql.server start
          Starting MySQL............ ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid). 

          這邊我們提供一個解決方案的鏈接:解決方案鏈接

          還可能會出現(xiàn):

          #還可能會出現(xiàn)
          Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

          這邊提供另一個解決方案:解決方案鏈接

          重啟服務(wù):

          [root@VM-0-10-centos bin]# /usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64/support-files/mysql.server start
          Starting MySQL SUCCESS! 


          #測試下命令行登錄
          [root@VM-0-10-centos bin]# mysql -u root -p
          Enter password:
          #到這一步直接回車,不比輸入密碼,因?yàn)閯偛乓呀?jīng)跳過密碼驗(yàn)證了

          MySQL [(none)]> exit;
          Bye
          [root@VM-0-10-centos bin] 

          第五步添加軟連接,方便重啟

          ln -s /usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64/bin/mysql /usr/bin/mysql
          ln -s /usr/local/mysql/mysql-5.7.24-linux-glibc2.12-x86_64/bin/mysql /usr/bin/mysql


          #添加完軟連接就可以在任意目錄下愉快地重啟了
          [root@VM-0-10-centos bin]# service mysql restart
          Shutting down MySQL.... SUCCESS! 
          Starting MySQL.......... SUCCESS! 


          第六步:登錄mysql,修改密碼

          #編輯my.cnf
          vim /etc/my.cnf
          #注釋掉

            #skip-grant-tables

          #重啟mysql
          service mysql restart


          mysql -u root -p
          mysql> use mysql;#使用數(shù)據(jù)庫
          mysql> set password for root@localhost = password('mysql123');#修改數(shù)據(jù)庫密碼為mysql123
          mysql> update user set user.Host='%' where user.User='root';#開放遠(yuǎn)程連接

          第七步:設(shè)置開機(jī)啟動

          1、將服務(wù)文件拷貝到init.d下,并重命名為mysql
          [root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
          2、賦予可執(zhí)行權(quán)限
          [root@localhost /]# chmod +x /etc/init.d/mysqld
          3、添加服務(wù)
          [root@localhost /]# chkconfig --add mysqld

          驗(yàn)證navicat遠(yuǎn)程連接

          2、Slave庫搭建(參考主庫搭建 ↑

          3、綁定主從關(guān)系(這一步主要是修改一些配置文件)

          (1)開啟master的二進(jìn)制日志

                  ①配置my.cnf配置文件

          vi /etc/my.cnf

                  ②添加二進(jìn)制日志配置,開啟二進(jìn)制(mysql-bin只是二進(jìn)制日志名稱,可以自行指定)

          server-id=1             #id是一定要指定的,是唯一的標(biāo)識(master數(shù)據(jù)庫要比slave數(shù)據(jù)庫的id優(yōu)先級高才行)
          log-bin=mysql-bin       #開啟二進(jìn)制日志

                 ③授權(quán) :登錄數(shù)據(jù)庫,需要給slave數(shù)據(jù)庫配置一個用戶/密碼的權(quán)限

                                 (允許某個ip地址的某個用戶以某個密碼對當(dāng)前數(shù)據(jù)庫的所有庫和表進(jìn)行復(fù)制操作配置之后需要刷新權(quán)限)

          mysql> grant replication slave on *.* to 'root'@'slave數(shù)據(jù)庫ip' identified by '密碼';
          mysql> flush privileges;

             ④修改完重啟服務(wù),查詢master狀態(tài)

          [root@VM_0_10_centos ~]# service mysqld restart

          登錄數(shù)據(jù)庫,查詢master狀態(tài),如下圖所示:

          mysql> show master status;

          file:是日志文件名稱

          position:日志所在位置

           

          (2) 開啟slave的二進(jìn)制日志

              ①修改my.cnf

           vi /etc/my.cnf


            添加slave二進(jìn)制日志配置,開啟二進(jìn)制(mysql-bin只是二進(jìn)制日志名稱,可以自行指定)

          server-id=2
          log-bin=mysql-bin

          注意:每一臺指定唯一的一個server-id標(biāo)識

          修改完配置服務(wù)需重啟服務(wù)

          [root@VM_0_16_centos ~]# service mysqld restart

             ②配置slave指向master,登錄數(shù)據(jù)庫

          mysql> change master to master_host='10.0.33.18',master_port=3306,master_user='root',master_password='mysql123',master_log_file='mysql-bin.000002',master_log_pos=154;

          mysql>flush privileges;

          mysql>start slave;

          mysql> show slave status\G;

          *************************** 1. row ***************************
                         Slave_IO_State: 
                            Master_Host: 10.0.3x.xx
                            Master_User: root
                            Master_Port: 3306
                          Connect_Retry: 60
                        Master_Log_File: mysql-bin.000002
                    Read_Master_Log_Pos: 154
                         Relay_Log_File: localhost-relay-bin.000002
                          Relay_Log_Pos: 4
                  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: 154
                        Relay_Log_Space: 154
                        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: NULL
          Master_SSL_Verify_Server_Cert: No
                          Last_IO_Errno: 2003
                          Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 9
                         Last_SQL_Errno: 0
                         Last_SQL_Error: 
            Replicate_Ignore_Server_Ids: 
                       Master_Server_Id: 0
                            Master_UUID: 
                       Master_Info_File: /usr/local/mysql/data/master.info
                              SQL_Delay: 0
                    SQL_Remaining_Delay: NULL
                Slave_SQL_Running_State: 
                     Master_Retry_Count: 86400
                            Master_Bind: 
                Last_IO_Error_Timestamp: 210419 15:17:11
               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)

          主要看這倆進(jìn)程是否YES:

          OK,主從復(fù)制配置文件到此修改完成。

          提示:若主從掛調(diào),可以優(yōu)先在這里查看報(bào)錯信息。

          4、測試主從復(fù)制效果

          二、讀寫分離配置

          1.初始化一個SpringBoot工程,并添加Sharding-jdbc依賴

          數(shù)據(jù)源配置代碼如下:

          sharding.jdbc.datasource.names=master,slave

          # 主數(shù)據(jù)源
          sharding.jdbc.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
          sharding.jdbc.datasource.master.driver-class-name=com.mysql.jdbc.Driver
          sharding.jdbc.datasource.master.url=jdbc:mysql://10.0.33.18:3308/sharding?useUnicode=true&characterEncoding=utf-8&useSSL=false
          sharding.jdbc.datasource.master.username=root
          sharding.jdbc.datasource.master.password=mysql123

          # 從數(shù)據(jù)源
          sharding.jdbc.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
          sharding.jdbc.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
          sharding.jdbc.datasource.slave.url=jdbc:mysql://10.0.33.19:3306/sharding?useUnicode=true&characterEncoding=utf-8&useSSL=false
          sharding.jdbc.datasource.slave.username=root
          sharding.jdbc.datasource.slave.password=mysql123

          # 讀寫分離配置
          sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
          sharding.jdbc.config.masterslave.name=dataSource
          sharding.jdbc.config.masterslave.master-data-source-name=master
          sharding.jdbc.config.masterslave.slave-data-source-names=slave

          2.測試讀寫分離效果

          如下,在mysql-master 容器中,查看sql日志,可以看到插入的sql在主數(shù)據(jù)庫執(zhí)行:

          如下,查看mysql-slave的sql日志,讀取列表數(shù)據(jù)在從數(shù)據(jù)庫執(zhí)行,說明我們配置的讀寫分離是成功的。

           




          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長按上方微信二維碼 2 秒


          感謝點(diǎn)贊支持下哈 

          瀏覽 51
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一区二区三区四区久久久 | 五月丁香小说色原网站 | 无码特级毛片 | 操操逼逼 | 日韩人妻久久亚洲 |