1. 程式人生 > 其它 >MySQL資料庫主從複製、讀寫分離和備份

MySQL資料庫主從複製、讀寫分離和備份

  1. 通過編譯、二進位制安裝MySQL5.7

    1、安裝相應工具包
    yum  -y install libaio numactl-libs wget
    
    2、準備啟動使用者
    groupadd mysql
    useradd -r -M -g mysql -s /bin/false mysql	# -r建立系統使用者,-M不建立使用者家目錄 -s指定使用者不可登入
    
    3、下載MySQL原始碼包解壓並修改許可權
    wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
    tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    cd /usr/local/
    ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
    chown -R mysql.mysql /usr/local/mysql/
    
    4、準備環境變數
    echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    
    5、準備配置檔案
    cp /etc/my.cnf{,.bak}
    vim /etc/my.cnf
    [mysqld]
    datadir=/data/mysql
    skip_name_resolve=1
    socket=/data/mysql/mysql.sock
    log-error=/data/mysql/mysql.log
    pid-file=/data/mysql/mysql.pid
    [client]
    socket=/data/mysql/mysql.sock
    
    6、初始化MySQL並設定root賬號密碼為空
    mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
    
    7、準備服務指令碼並啟動MySQL
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    
    8、設定root賬號密碼
    mysqladmin -uroot   password magedu
    
    9、測試登入
    mysql -uroot -pmagedu
    
  2. 二進位制安裝mariadb10.4

    1、下載二進位制包並新增啟動賬號
    wget https://mirrors.bkns.vn/mariadb//mariadb-10.4.25/bintar-linux-systemd-x86_64/mariadb-10.4.25-linux-systemd-x86_64.tar.gz
    groupadd mysql
    useradd -r -M -g mysql -s /bin/false mysql	# -r建立系統使用者,-M不建立使用者家目錄 -s指定使用者不可登入
    
    2、安裝mariadb-server
    tar -xf mariadb-10.4.25-linux-systemd-x86_64.tar.gz -C /usr/local
    cd /usr/local
    ln -s mariadb-10.4.25-linux-systemd-x86_64 mysql
    cd mysql
    ./scripts/mysql_install_db --user=mysql --datadir=/var/lib/mysql  #指定資料存放目錄,此處使用/etc/my.cnf中的預設配置
    chown -R mysql.mysql .
    
    3、啟動mysql並配置環境變數
    export PATH=$PATH:/usr/local/mysql/bin/   #臨時生效,永久生效配置參考MySQL5.7的安裝步驟
    
    4、準備服務開機自啟檔案
    cp support-files/systemd/mariadb.service /usr/lib/systemd/system/mariadb.service
    systemctl daemon-reload
    
    7、啟動mariadb-server
    systemctl start mariadb.service 
    
    8、設定開機自啟
    systemctl enable mariadb.service 
    
    9、連線mysql測試
    [root@localhost mysql]# mysql 	#直接輸入mysql報錯,此處是因為二進位制安裝的mariadb的客戶端命令mysql將/tmp/mysql.sock 作為預設路徑,而當前/etc/my.cnf配置的路徑為/var/lib/mysql/mysql.sock
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    
    手動指定sock檔案路徑即可登入成功(sock檔案的作用讀者可自行查閱相關資料)
    [root@localhost mysql]# mysql --sock=/var/lib/mysql/mysql.sock
    Info: Using unique option prefix 'sock' is error-prone and can break in the future. Please use the full name 'socket' instead.
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 9
    Server version: 10.4.25-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> 
    
    
  3. 匯入hellodb.sql生成資料庫
    (1) 在students表中,查詢年齡大於25歲,且為男性的同學的名字和年齡

    MariaDB [hellodb]> desc students;         --查看錶結構
    +-----------+---------------------+------+-----+---------+----------------+
    | Field     | Type                | Null | Key | Default | Extra          |
    +-----------+---------------------+------+-----+---------+----------------+
    | StuID     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
    | Name      | varchar(50)         | NO   |     | NULL    |                |
    | Age       | tinyint(3) unsigned | NO   |     | NULL    |                |
    | Gender    | enum('F','M')       | NO   |     | NULL    |                |
    | ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |
    | TeacherID | int(10) unsigned    | YES  |     | NULL    |                |
    +-----------+---------------------+------+-----+---------+----------------+
    6 rows in set (0.00 sec)
    
    MariaDB [hellodb]> select name,age from students where age >25 and gender='M';
    +--------------+-----+
    | name         | age |
    +--------------+-----+
    | Xie Yanke    |  53 |
    | Ding Dian    |  32 |
    | Yu Yutong    |  26 |
    | Shi Qing     |  46 |
    | Tian Boguang |  33 |
    | Xu Xian      |  27 |
    | Sun Dasheng  | 100 |
    +--------------+-----+
    7 rows in set (0.00 sec)
    

    (2) 以ClassID為分組依據,顯示每組的平均年齡

    MariaDB [hellodb]> select classid,avg(age) from students group by classid;
    +---------+----------+
    | classid | avg(age) |
    +---------+----------+
    |    NULL |  63.5000 |
    |       1 |  20.5000 |
    |       2 |  36.0000 |
    |       3 |  20.2500 |
    |       4 |  24.7500 |
    |       5 |  46.0000 |
    |       6 |  20.7500 |
    |       7 |  19.6667 |
    +---------+----------+
    8 rows in set (0.00 sec)
    

    (3) 顯示第2題中平均年齡大於30的分組及平均年齡

    MariaDB [hellodb]> select classid,avg(age) avg_age from students group by classid having avg_age > 30;
    +---------+---------+
    | classid | avg_age |
    +---------+---------+
    |    NULL | 63.5000 |
    |       2 | 36.0000 |
    |       5 | 46.0000 |
    +---------+---------+
    3 rows in set (0.00 sec)
    

    (4) 顯示以L開頭的名字的同學的資訊

    MariaDB [hellodb]> select * from students where name like 'L%';
    +-------+-------------+-----+--------+---------+-----------+
    | StuID | Name        | Age | Gender | ClassID | TeacherID |
    +-------+-------------+-----+--------+---------+-----------+
    |     8 | Lin Daiyu   |  17 | F      |       7 |      NULL |
    |    14 | Lu Wushuang |  17 | F      |       3 |      NULL |
    |    17 | Lin Chong   |  25 | M      |       4 |      NULL |
    +-------+-------------+-----+--------+---------+-----------+
    3 rows in set (0.00 sec)
    
  4. 資料庫授權root使用者,允許172.16.16.0/24網段可以連線mysql(注意關閉防火牆)
    檢視當前資料庫使用者,觀察到沒有授權遠端登陸賬號

    建立使用者root@'172.16.16.%' 並設定密碼後驗證登陸

  5. 主從複製及主主複製的實現(注意關閉防火牆)
    主從複製

    系統:centos7.6  資料庫 5.5.68-MariaDB 
    
    主節點配置(172.16.16.128):
    systemctl stop firewalld
    yum install -y mariadb-server   #注意:centos7.6系統官方倉庫未提供mysql-server源,因此安裝mariadb作為代替
    
    [root@master ~]# cat /etc/my.cnf.d/server.cnf 
    [mysqld]
    server-id=8
    log-bin
    
    [root@master ~]# mysql
    MariaDB [(none)]> show master logs;  --檢視二進位制檔案位置
    +--------------------+-----------+
    | Log_name           | File_size |
    +--------------------+-----------+
    | mariadb-bin.000001 |       418 |
    | mariadb-bin.000002 |       688 |
    +--------------------+-----------+
    2 rows in set (0.00 sec)
    MariaDB [(none)]> grant replication slave on *.* to repluser@'172.16.16.%' identified by 'passwd'; --#新建賬號 repluser密碼passwd並授權
    Query OK, 0 rows affected (0.00 sec)
    
    --如果是MySQL 8.0 建立賬號和授權需要分成下面兩步實現
    mysql>create user 'repluser'@'172.16.16.%';
    mysql>grant replication slave on *.* to 'repluser'@'172.16.16.%';
    
    
    從節點配置(172.16.16.28):
    systemctl stop firewalld
    [root@slave ~]# cat /etc/my.cnf.d/server.cnf 
    [mysqld]
    server-id=18
    log-bin
    
    systemctl start mariadb-server
    [root@slave ~]# mysql
    MariaDB [(none)]> change master to 
    	-> master_host='172.16.16.128',
    	-> master_user='repluser',
    	-> master_password='passwd',
    	-> master_port=3306,
    	-> master_log_file='mariadb-bin.000002',
    	-> master_log_pos=245;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> show slave status\G;
    *************************** 1. row ***************************
    			   Slave_IO_State: Waiting for master to send event
    				  Master_Host: 172.16.16.128
    				  Master_User: repluser
    				  Master_Port: 3306
    				Connect_Retry: 60
    			  Master_Log_File: mariadb-bin.000002
    		  Read_Master_Log_Pos: 996
    			   Relay_Log_File: mariadb-relay-bin.000002
    				Relay_Log_Pos: 839
    		Relay_Master_Log_File: mariadb-bin.000002
    			 Slave_IO_Running: Yes	--I/O執行緒正常執行
    			Slave_SQL_Running: Yes	--sql執行緒正常執行
    			  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: 996
    			  Relay_Log_Space: 1135
    			  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	--主從複製延遲為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: 8
    1 row in set (0.00 sec)
    
  6. xtrabackup實現全量+增量+binlog恢復庫

  7. MyCAT實現MySQL讀寫分離