MySQL多實例(二)


一、多實例MySQL數據庫原理

  1.1:原理圖

 

    

  1.2:多實例原理(什么是多實例)

    簡單來說MySQL多實例就是在一台服務器上同時開啟多個不用的服務端口(如:3306、3307),同時運行多個MySQL進程,這些服務進程通過不同的socket監聽不同的服務端口來提供服務,這些MySQL多實例公用一套MySQL安裝程序,使用不同的my.cnf配置文件、啟動程序和數據文件。在提供服務時,多實例MySQL在邏輯上看來是各自獨立的,它們根據配置文件的對應設定值,獲取服務器的相應數量的硬件資源。

二、多實例MySQL數據庫優缺點及應用場景

  2.1:多實例的優點

    ① 有效的利用資源

    ② 節約服務器資源

 

  2.2:多實例的缺點

    ① 會存在資源互相搶占的問題(當某個數據庫並發很高或者sql慢查詢的時候,整個實例會消耗大量的CPU、磁盤I/O 導致服務器的其他數據庫實例提供服務的質量一起下降)

 

  2.2:多實例的應用場景

    ① 資金緊張型公司(公司資金緊張,業務訪問量不大,又希望數據庫服務隔離,同時還需要主從復制提供備份或者讀寫分離)

    ② 並發訪問不是特別大的業務

    ③ 門戶網站應用(服務器配置好,可以節省IDC空間,同時又充分利用的服務器的資源)

三、多實例MySQL數據庫實現方案

  3.1:單一配置文件、單一啟動程序多實例部署方案(耦合度很高,也是官網的推薦)

  3.2:多配置文件、多啟動啟動文件、多數據文件(解決了耦合度高的問題)

四、多實例MySQL數據庫安裝

 PS:和單實例安裝的方式是基本上差不多的,我很久采用二進制的安裝方式來進行多實例的安裝,單實例安裝地址:https://www.cnblogs.com/zhujingzhi/p/9609861.html

  4.1:3306實例安裝

    ① 下載MySQL卸載自帶Mariadb

[root@web1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@web1 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
mariadb-devel-5.5.56-2.el7.x86_64

[root@web1 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 mariadb-devel-5.5.56-2.el7.x86_64

    ② 安裝配置MySQL3306

[root@web1 ~]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/                         # 解壓MySQL
[root@web1 ~]# useradd -M -s /sbin/nologin mysql                                                        # 創建MySQL用戶

[root@web1 usr]# cd /usr/local/ 																			
[root@web1 local]# mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql/ 										 	

# 創建多實例的數據文件和日志文件
[root@web1 opt]# cd /home/
[root@web1 local]# mkdir -p /home/mysql/{3306,3307}/{data,log}

# 配置mysql3306的配置文件
[root@web1 3306]# vim my.cnf
[client]
port            = 3306
socket          = /home/mysql/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
socket  = /home/mysql/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /home/mysql/3306/data
pid-file = /home/mysql/3306/mysql.pid
open_files_limit    = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
log-error = /home/mysql/3306/log/error.log
#log-slow-queries = /home/mysql/3306/log/slow.log
pid-file = /home/mysql/3306/mysql.pid
#log-bin = /home/mysql/3306/mysql-bin
relay-log = /home/mysql/3306/relay-bin
relay-log-info-file = /home/mysql/3306/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M


lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql

server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

innodb_buffer_pool_size = 32M
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0


[mysqldump]
quick
max_allowed_packet = 2M

# 這里說明一下為什么要先創建這個error文件,因為這個是數據庫的一個bug在啟動數據庫的時候會出現
2018-09-10T06:29:10.161125Z mysqld_safe error: log-error set to '/home/mysql/3306/log/error.log', however file don't exists. Create writable for user 'mysql'.
[root@web1 mysql]# touch /home/mysql/3306/log/error.log
[root@web1 mysql]# touch /home/mysql/3307/log/error.log [root@web1 mysql]# chown -R mysql.mysql /home/mysql/ # 設置MySQL權限 [root@web1 3306]# yum install -y libaio # 必須安裝不然會報下面的錯誤 ./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory # 設置環境比變量 [root@web1 mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@web1 mysql]# source /etc/profile # 是環境變量生效

    ③ 修改密碼及啟動MySQL3306

# 初始化數據庫,要記住初始化的密碼
[root@web1 3306]# cd /usr/local/mysql/
[root@web1 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/3306/data
2018-09-10T03:09:58.040768Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-10T03:09:58.452316Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-10T03:09:58.515904Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-10T03:09:58.582330Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ffe8e1c7-b4a6-11e8-91ba-000c296f1735.
2018-09-10T03:09:58.584298Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-10T03:09:58.585110Z 1 [Note] A temporary password is generated for root@localhost: (RKsr0Jv(H9v    # 初始化密碼

# 因為5.7的安全機制必須要修改默認密碼才能使用數據庫,所以我們要先使用mysqld_safe啟動數據庫修改密碼
[root@web1 3306]# cd /usr/local/mysql/
[root@web1 mysql]# ./bin/mysqld_safe  --defaults-file=/home/mysql/3306/my.cnf --user=mysql     # 啟動3306數據庫
[root@web1 mysql]# ./bin/mysql -u root -p -S /home/mysql/3306/mysql.sock
Enter password: (RKsr0Jv(H9v   # 初始化密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23
 
Copyright (c) 2000, 2018, 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 FOR 'root'@'localhost' = PASSWORD('123456');   # 修改密碼
Query OK, 0 rows affected, 1 warning (0.00 sec)



# 編寫MySQL啟動腳本

[root@web1 3306]# vim /etc/init.d/mysql3306
#!/bin/sh

#init
port=3306
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"
mysql_sock="/home/mysql/${port}/mysql.sock"
#startup function
function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/home/mysql/${port}/my.cnf --user=mysql 2>&1 > /dev/null &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

#stop function
function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /home/mysql/${port}/mysql.sock shutdown
   fi
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /home/mysql/${port}/mysql {start|stop|restart}\n"
esac




# 啟動
[root@web1 mysql]# /etc/init.d/mysql3306 start

# 停止,為了安全這里需要密碼
[root@web1 mysql]# /etc/init.d/mysql3307 stop
Stoping MySQL...
Enter password: 123456  # 數據庫密碼

    ④ 設置遠程數據庫權限

# 設置遠程主機登錄權限
[root@web1 mysql]# mysql -uroot -p -S /home/mysql/3306/mysql.sock
Enter password:123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 all privileges on *.* to 'root' @'%' identified by '123456';   # 設置權限
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;													# 刷新權限

  4.2:3307實例安裝

  PS:和配置3306一樣只是修改一下端口 把3306改成3307

    ① 修改配置文件

# 拷貝配置文件
[root@web1 ~]# cp /home/mysql/3306/my.cnf /home/mysql/3307/

# 修改配置文件
[root@web1 ~]# sed -i "s#3306#3307#g" /home/mysql/3307/my.cnf

# 查看配置文件
[root@web1 ~]# cat /home/mysql/3307/my.cnf

    ② 修改啟動腳本

# 拷貝啟動文件
[root@web1 mysql]# cp /etc/init.d/mysql3306 /etc/init.d/mysql3307

# 修改啟動文件
[root@web1 mysql]# sed -i "s#3306#3307#g" /etc/init.d/mysql3307

# 查看啟動文件
[root@web1 mysql]# cat /etc/init.d/mysql3307

    ③ 初始化3307數據庫

# 設置mysql權限
[root@web1 3307]# chown mysql.mysql /home/mysql/
# 初始化數據庫
[root@web1 3307]# cd /usr/local/mysql/
[root@web1 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/3307/data
2018-09-10T07:02:06.000207Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-10T07:02:06.452043Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-10T07:02:06.498280Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-10T07:02:06.556171Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6da1081a-b4c7-11e8-be64-000c296f1735.
2018-09-10T07:02:06.557543Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-10T07:02:06.558513Z 1 [Note] A temporary password is generated for root@localhost: _ob9Hqq9:?kk

    ④ 啟動3307數據庫及修改密碼

# 啟動數據庫
[root@web1 mysql]# cd /usr/local/mysql/
[root@web1 mysql]# ./bin/mysqld_safe  --defaults-file=/home/mysql/3307/my.cnf --user=mysql 

[root@web1 mysql]# mysql -uroot -p_ob9Hqq9:?kk -S /home/mysql/3307/
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 2
Server version: 5.7.23

Copyright (c) 2000, 2018, 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 FOR 'root'@'localhost' = PASSWORD('123456');    # 修改密碼
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;    # 刷新
Query OK, 0 rows affected (0.00 sec)

五、查看啟動是否成功

[root@web1 ~]# netstat -lntup | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      20490/mysqld
tcp6       0      0 :::3307                 :::*                    LISTEN      55606/mysqld
[root@web1 ~]#

六、多實例MySQL數據庫登錄方式

# 登錄3306數據庫
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3306/mysql.sock
Enter password:123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>



# 登錄3307數據庫
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3307/mysql.sock
Enter password:123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM