1. 程式人生 > >Mysql 5.7.21單機多實例安裝

Mysql 5.7.21單機多實例安裝

用戶 files inter .gz address roo href bin rec

  1. 下載MySQL 5.7 二制包
    [root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz

  2. 解壓 MySQL 5.7 二進制包到指定目錄
    [root@MySQL ~]# tar zxvf mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

  3. 創建 MySQL 軟鏈接
    [root@MySQL ~]# ln -s /usr/local/mysql-5.7.21-linux-glibc2.5-x86_64 /usr/local/mysql

  4. 創建 MySQL 用戶
    [root@MySQL ~]# useradd -r -s /sbin/nologin mysql

  5. 在 MySQL 二進制包目錄中創建 mysql-files 目錄 [MySQL 數據導入/導出數據專放目錄]

    [root@MySQL ~]# mkdir -v /usr/local/mysql/mysql-files 
    mkdir: created directory `/usr/local/mysql/mysql-files‘
  6. 創建多實例數據目錄

    [root@MySQL ~]# mkdir -vp /data/mysql_data{1..4} 
    mkdir: created directory `/data‘ 
    mkdir: created directory `/data/mysql_data1‘ 
    mkdir: created directory `/data/mysql_data2‘ 
    mkdir: created directory `/data/mysql_data3‘ 
    mkdir: created directory `/data/mysql_data4‘
  7. 修改 MySQL 二進制包目錄的所屬用戶與所屬組
    [root@MySQL ~]# chown root.mysql -R /usr/local/mysql-5.7.21-linux-glibc2.5-x86_64

  8. 修改 MySQL 多實例數據目錄與數據導入/導出專放目錄的所屬用戶與所屬組
    [root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql/mysql-files /data/mysql_data{1..4}

  9. 配置 MySQL 配置文件 /etc/my.cnf

    [mysqld_multi] 
    mysqld    = /usr/local/mysql/bin/mysqld  
    mysqladmin = /usr/local/mysql/bin/mysqladmin
    log        = /tmp/mysql_multi.log 
    [mysqld1] 
    # 設置數據目錄 [多實例中一定要不同] 
    datadir = /data/mysql_data1
    # 設置sock存放文件名 [多實例中一定要不同] 
    socket = /tmp/mysql.sock1 
    # 設置監聽開放端口 [多實例中一定要不同] 
    port = 3306 
    # 設置運行用戶 
    user = mysql 
    # 關閉監控 
    performance_schema = off 
    # 設置innodb 緩存大小 
    innodb_buffer_pool_size = 32M 
    # 設置監聽IP地址 
    bind_address = 0.0.0.0 
    # 關閉DNS 反向解析 
    skip-name-resolve = 0 
    [mysqld2] 
    datadir = /data/mysql_data2
    socket = /tmp/mysql.sock2 
    port = 3307 
    user = mysql 
    performance_schema = off 
    innodb_buffer_pool_size = 32M 
    bind_address = 0.0.0.0 
    skip-name-resolve = 0   
    [mysqld3] 
    datadir = /data/mysql_data3
    socket = /tmp/mysql.sock3 
    port = 3308 
    user = mysql 
    performance_schema = off 
    innodb_buffer_pool_size = 32M 
    bind_address = 0.0.0.0 
    skip-name-resolve = 0 
    [mysqld4] 
    datadir = /data/mysql_data4
    socket = /tmp/mysql.sock4 
    port = 3309 
    user = mysql 
    performance_schema = off 
    innodb_buffer_pool_size = 32M 
    bind_address = 0.0.0.0 
    skip-name-resolve = 0
  10. 初始化各個實例 [ 初始化完成後會自帶隨機密碼在輸出日誌中 ]

    [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1 
    [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2 
    [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3 
    [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4
  11. 各實例開啟 SSL 連接

    [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1 
    [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2 
    [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3 
    [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4
  12. 復制多實例腳本到服務管理目錄下 [ /etc/init.d/ ]
    [root@MySQL ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi

  13. 添加腳本執行權限
    [root@MySQL ~]# chmod +x /etc/init.d/mysqld_multi

  14. 添加進service服務管理
    [root@MySQL ~]# chkconfig --add mysqld_multi

  15. 啟動測試
  • 查個多實例狀態

    [root@MySQL ~]# /etc/init.d/mysqld_multi report 
    Reporting MySQL servers 
    MySQL server from group: mysqld1 is not running 
    MySQL server from group: mysqld2 is not running 
    MySQL server from group: mysqld3 is not running 
    MySQL server from group: mysqld4 is not running
  • 啟動多實例
    [root@MySQL ~]# /etc/init.d/mysqld_multi start

  • 查看多實例狀態

    Reporting MySQL servers 
    MySQL server from group: mysqld1 is running 
    MySQL server from group: mysqld2 is running 
    MySQL server from group: mysqld3 is running 
    MySQL server from group: mysqld4 is running
  • 查看實例監聽端口

    [root@MySQL ~]# netstat -lntp | grep mysqld 
    tcp        0      0 0.0.0.0:3306        0.0.0.0:*                LISTEN      2673/mysqld        
    tcp        0      0 0.0.0.0:3307        0.0.0.0:*                LISTEN      2676/mysqld        
    tcp        0      0 0.0.0.0:3308        0.0.0.0:*                LISTEN      2679/mysqld        
    tcp        0      0 0.0.0.0:3309        0.0.0.0:*                LISTEN      2682/mysqld
  1. 連接測試
  • 實例1

    [root@MySQL ~]# /usr/local/mysql/bin/mysql -S /tmp/mysql.sock1  -p‘z+Ilo*>s:3kw‘ 
    mysql: [Warning] Using a password on the command line interface can be insecure. 
    Welcome to the MySQL monitor.  Commands end with ; or \g. 
    Your MySQL connection id is 6 
    Server version: 5.7.18 
    Copyright (c) 2000, 2017, 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 password = ‘123456‘; 
    Query OK, 0 rows affected (0.00 sec)
  • 實例2

    [root@MySQL ~]# /usr/local/mysql/bin/mysql -S /tmp/mysql.sock2  -p‘b*AHUrTgu1rl‘ 
    mysql: [Warning] Using a password on the command line interface can be insecure. 
    Welcome to the MySQL monitor.  Commands end with ; or \g. 
    Your MySQL connection id is 7 
    Server version: 5.7.18 
    Copyright (c) 2000, 2017, 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 password = ‘123456‘; 
    Query OK, 0 rows affected (0.00 sec)

    原文鏈接:https://www.linuxidc.com/Linux/2017-07/145343.htm

Mysql 5.7.21單機多實例安裝