1、MySQL多實例介紹mysql
一臺服務器上開啓多個不一樣的服務端口(3306,3307,3308),運行多個MySQL服務進程,共用一套MySQL安裝程序,多實例MySQL在邏輯上看是各自獨立的linux
一、多實例主從複製原理圖sql
二、優勢數據庫
①有效利用服務器資源
②節約服務器資源
③資源互相搶佔問題,其中一個實例併發很高或者慢查詢,其它實例也受影響服務器
三、應用場景併發
①公司資金緊缺
②併發訪問不是特別大的業務
③門戶網站應用MySQL多實例場景,通常是從庫app
四、MySQL多實例常見配置方案socket
①多配置文件、多啓動程序的方案(經常使用)
②單一配置文件、單一啓動程序多實例方案(一個配置文件,很差管理,耦合度過高,)tcp
2、MySQL多實例部署網站
一、環境
[root@MySQL ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@MySQL ~]# uname -r 2.6.32-696.el6.x86_64 [root@MySQL ~]# hostname -I 172.19.5.54 172.16.1.54
二、安裝MySQL
useradd -s /sbin/nologin -M mysql cd /server/tools tar xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.36-linux-glibc2.5-x86_64 /application/mysql-5.6.36 ln -s /application/mysql-5.6.36/ /application/mysql chown -R mysql.mysql /application/mysql
三、配置多實例啓動命令和配置文件目錄
mkdir -p /data/{3306,3307}/data
四、unzip data.zip 在/下直接解壓覆蓋/data目錄(注:data.zip是打包好的多實例配置文件、數據、啓動命令)
[root@MySQL data]# tree . ├── 3306 │ ├── data #3306實例數據文件目錄 │ ├── my.cnf #3306實例配置文件 │ └── mysql #3306實例啓動命令 └── 3307 ├── data #3307實例數據文件目錄 ├── my.cnf #3307實例配置文件 └── mysql #3307實例啓動命令
五、受權mysql用戶管理/data目錄
chown -R mysql.mysql /data
六、受權mysql命令執行權限
find /data -type f -name "mysql"|xargs chmod +x
七、配置mysql命令啓動環境變量
方法一:
ln -s /application/mysql/bin/* /usr/local/sbin/ 方法二: echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile source /etc/profile
八、初始化多實例數據庫
cd /application/mysql/scripts/ ./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3306/data/ --user=mysql #初始化數據庫3306 ./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql #初始化數據庫3307
九、MySQL多實例服務啓動
#啓動服務 /data/3306/mysql start /data/3307/mysql start [root@MySQL data]# netstat -lntup|grep 330 tcp 0 0 :::3306 :::* LISTEN 3848/mysqld tcp 0 0 :::3307 :::* LISTEN 4885/mysqld
十、MySQL多實例設置密碼及登陸
#設置密碼: mysqladmin -uroot password 123456 -S /data/3306/mysql.sock mysqladmin -uroot password 123456 -S /data/3307/mysql.sock #登陸數據庫,指定sock mysql -uroot -p123456 -S /data/3306/mysql.sock mysql -uroot -p123456 -S /data/3307/mysql.sock
3、MySQL多實例配置文件
/data/3306/my.cnf [client] port = 3306 socket = /data/3306/mysql.sock [mysqld] port = 3306 socket = /data/3306/mysql.sock datadir = /data/3306/data
open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_open_cache = 512 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 pid-file = /data/3306/mysql.pid log-bin = /data/3306/mysql-bin relay-log = /data/3306/relay-bin relay-log-info-file = /data/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 = 4 |
/data/3307/my.cnf [client] port = 3307 socket = /data/3307/mysql.sock [mysqld] port = 3307 socket = /data/3307/mysql.sock datadir = /data/3307/data
open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_open_cache = 512 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 2M max_heap_table_size = 2M long_query_time = 1 pid-file = /data/3307/mysql.pid relay-log = /data/3307/relay-bin relay-log-info-file = /data/3307/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 = 5 |
說明:對比兩個配置文件以後,能夠發現除了端口、server-id不同,3306開啓了log-bin功能,這樣能夠作MySQL主複製
4、MySQL多實例啓動命令腳本
#!/bin/sh #init port=3306 mysql_user="root" mysql_pwd="123456" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 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 /data/${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: /data/${port}/mysql {start|stop|restart}\n" esac
5、遇到的錯誤
一、[ERROR] Plugin 'InnoDB' init function returned error
刪除MySQL目錄下的ib_logfile0和ib_logfile1兩個文件,就能夠解決問題了
二、5.5.32——>5.6.36配置參數不對致使多實例沒法啓動
table_cache——>table_open_cache = 512(5.6更名了)
6、生產環境多實例配置環境
一、通常在1-4個實例之間居多,1-2個最多,大業務佔用的機器比較多,機器R510居多,CPU是E5210,48G內存,硬盤12*300G SAS,RAID10
二、內存32G,雙cpu8核,硬盤6X600G,跑2-3個實例