什么是Mysql的多實例?
簡單的說,Mysql多實例就是一台服務器上同時開啟多個不同的服務端口(如3306、3307)同時運行多個Mysql服務進程,這些服務進程通過不同socket監聽不同的服務端口來提供服務。
Msyql的多實例特點:
這些Mysql共用一套Mysql的安裝程序,使用不用的配置文件(my.cnf)啟動程序和數據文件。在提供服務時,多實例Mysql在邏輯上看起來是各自獨立的,它們根據配置文件的對應設定值,獲得服務器響應數量的硬件資源。
補充:
其實很多網絡服務器都是可以配置多實例的,例如Nginx Apache Redis Memcache 都可以配置多實例。
mysql多實例的作用:
1)有效的利用服務器資源 當單個服務器資源有剩余的時候,可以充分利用剩余的資源提供更多的服務,且可以實現資源的邏輯隔離。 2)節約服務器資源 當公司資金緊張,但是數據庫又需要各自盡量獨立提供服務,而且,需要主從復制等多實例就再好不過了。 但是 mysql多實例也有弊端,當某個數據庫實例並發很高或者SQL滿查詢時,會存在資源相互搶占的問題。
Mysql多實例的生產應用場景
1.資金緊張型公司的選擇
比如: 3台服務器部署9-15個實例,交叉做主從復制,數據備份及讀寫分離,這樣就可以達到9-15台服務器每個只裝一個數據庫才有的效果。所謂的獨立是相對的。
2.並發訪問不是很大的業務
當公司業務量不太大時候,服務器的資源基本都是浪費的,這時就很適合多實例的應用,如果對SQL語句的優化做的比較好,Mysql多實例會是一個很值得使用的技術,即可並發很大,合理分配系統資源以及搭配好服務,也不會有太大問題。
3.門戶網站應用Mysql多實例場景
例如:百度公司,某部門中IBM服務器為48核CPU,內存96GB 。這樣配置服務器可以跑3-4個實例;sina網也是用的多實例,內存48G左右。
補充:sina網的數據庫單機1-4數據庫實例居多,其中多數1-2個的最多,因為大業務占用的機器多。服務器是DELL R510居多,CPU E5210 48G內存 硬盤 12*300G SAS 做RAID10 ;另外,sina網站安裝數據庫時,一般采用編譯安裝方式安裝,並在做好優化之后定制成rpm包,以便統一使用.
Mysql多實例配置方案
1.單一配置文件,單一啟動程序多實例部署方案
Mysql官方文檔提到的單一配置文件,單一啟動程序多實例部署實例,不推薦此方案,這里作為知識點提及。官方my.cnf配置文件示例配置如下:
[mysqld_multil] mysqld = /usr/bin/mysqld_safe mysqladmin = /usr/bin/mysqladmin user = mysql [mysqld1] socket = /var/lib/mysql/mysql.sock port = 3306 pid-file = /var/lib/mysql/mysql.pid datadir = /var/lib/mysql/ user = mysql [mysqld2] socket =/mnt/data/db1/mysql.sock port = 3302 pid-file = /mnt/data/db1/mysql.pid datadir = /mnt/data/db1/ user = mysql skip-name-resolve server-id=10 dafault-storage-engine=innodb innodb_buffer_pool_size=512M innodb_additional_mem_pool=10M default_character_set=utf8 character_set_server=utf8 #read-only relay-log-space-limit=3G expire_logs_day=20
啟動命令如下:mysqld_multi --config-file=/data/mysql/my_multi.cnf start 1,2
對於該方案,缺點是耦合度太高,一個配置文件,不好管理,工作開發和運維的統一原則,降低耦合度。
2.多配置文件多啟動程序部署方案
多配置文件,多啟動程序部署方案,是推薦的方案。下面來看配置示例:
[root@Mysql /]tree /data /data |-- 3306 | |--data 《==3306實例的數據文件 | |--my.cnf 《==3306實例的配置文件 | |--mysql 《==3306實例的啟動文件 --3307 | |--data 《==3307實例的數據文件 | |--my.cnf 《==3307實例的配置文件 | |--mysql 《==3307實例的啟動文件
Mysql多實例配置流程:
1.安裝Msyql需要的依賴包 2.編譯安裝Mysql需要的軟件 3.開始安裝Mysql 4.配置多實例 5.初始化Mysql數據庫 6.啟動Mysql 7.登錄mysql 8.故障排除說明 9.開機自啟動
Mysql多實例數據庫安裝
1.安裝Msyql需要的依賴包
yum install ncurses-devel libaio-devel -y rpm -qa ncurses-devel libaio-devel libaio-devel-0.3.107-10.e16x86_64 ncurses-devel-5.7-3.20090208.c16x86_64
數據庫:
服務器名字 IP地址 host C3-Mysql數據庫 172.16.1.52/24 db02
2.編譯安裝Mysql需要的軟件
ls -lh cmake-2.8.8.tar.gz tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./configure gmake gmake install which cmake /usr/local/bin/cmake cd ../
或者也可以使用yum安裝 (推薦yum安裝)yum install cmake -y
3.開始安裝Mysql
#創建mysql用戶 useradd mysql -s /sbin/nologin -M id mysql #進入程序下載的路徑 cd /home/oldboy/tools wget http://downloads.mysql.com/archives/get/file/mysql-5.5.32.tar.gz
Mysql源碼包和二進制包名稱
Mysql軟件 軟件名 Msyql源碼安裝包 mysql-5.5.32.tar.gz(本文使用的安裝包) Mysql二進制安裝包 mysql-5.5.32-linux-x86_64.tar.gz
#解壓安裝包
tar xf mysql-5.5.32.tar.gz cd mysql-5.5.32 [root@db02 mysql-5.5.32]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \ -DMYSQL_DATADIR=/application/mysql-5.5.32/data \ -DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \ -DENABLED_LOCAL_INFILE=ON \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FAST_MUTEXES=1 \ -DWITH_ZLIB=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_READLINE=1 \ -DWITH_EMBEDDED_SERVER=1 \ -DWITH_DEBUG=0 [root@db02 mysql-5.5.32]# make && make install [root@db02 mysql-5.5.32]# ln -s /application/mysql-5.5.32/ /application/mysql [root@db02 mysql-5.5.32]# cd /application/mysql [root@db02 mysql]# ls bin data include lib mysql-test scripts sql-bench COPYING docs INSTALL-BINARY man README share support-files
4.配置多實例
[root@db02 mysql]# mkdir -p /data/{3306,3307}/data [root@db02 ~]# tree /data/ /data/ ├── 3306 │ ├── data │ ├── my.cnf │ └── mysql └── 3307 ├── data ├── my.cnf └── mysql 4 directories, 4 files
3306文件夾說明:
####vim my.cnf####
[client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3306 socket = /data/3306/mysql.sock basedir = /application/mysql datadir = /data/3306/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 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 #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 = /data/3306/error.log #log-slow-queries = /data/3306/slow.log 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 #myisam_sort_buffer_size = 1M #myisam_max_sort_file_size = 10G #myisam_max_extra_sort_file_size = 10G #myisam_repair_threads = 1 #myisam_recover lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db=mysql server-id = 1 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 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 [mysqld_safe] log-error=/data/3306/mysql_oldboy3306.err pid-file=/data/3306/mysqld.pid
####vim mysql####
#!/bin/sh ################################################ #this scripts is created by liuliya at 2016-12-09 #oldboy QQ:865205026 #site:http://www.liuliya.com ################################################ #init port=3306 mysql_user="root" mysql_pwd="oldboy" 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
3307文件夾說明:
####vim my.cnf
[client] port = 3307 socket = /data/3307/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql port = 3307 socket = /data/3307/mysql.sock basedir = /application/mysql datadir = /data/3307/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 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 #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 = /data/3307/error.log #log-slow-queries = /data/3307/slow.log pid-file = /data/3307/mysql.pid #log-bin = /data/3307/mysql-bin 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 #myisam_sort_buffer_size = 1M #myisam_max_sort_file_size = 10G #myisam_max_extra_sort_file_size = 10G #myisam_repair_threads = 1 #myisam_recover lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db=mysql server-id = 3 innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 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 [mysqld_safe] log-error=/data/3307/mysql_oldboy3307.err pid-file=/data/3307/mysqld.pid
####vim mysql####
#!/bin/sh ################################################ #this scripts is created by liuliya at 2016-12-09 #oldboy QQ:865205026 #site:http://www.liuliya.com ################################################ #init port=3307 mysql_user="root" mysql_pwd="oldboy" 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
#為mysql啟動命令增加執行權限find /data -type f -name "mysql"|xargs chmod +x
#更改/data目錄的屬組chwon -R mysql.mysql /data/
5.初始化Mysql數據庫(mysql加載自身的管理的庫和表)
[root@db02 ~]# cd /application/mysql/scripts/ [root@db02 scripts]# ls mysql_install_db
#3306實例初始化[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
返回2個OK證明成功
#3307實例初始化[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
返回2個OK證明成功
6.啟動Mysql
[root@db02 scripts]# /data/3306/mysql start [root@db02 scripts]# /data/3307/mysql start
#查看端口netstat -lntup|grep 330
#配置mysql的環境變量cp /application/mysql/bin/* /usr/local/sbin/
#查看環境變量echo $PATH
7.登錄mysql(需要制定sock)
mysql -S /data/3306/mysql.sock mysql -S /data/3307/mysql.sock
8.故障排除說明
1.查看日志cat mysql_oldboy3306.err
2.啟動如果發現沒有顯示Msyql對應實例的端口,請稍微等待幾秒在檢查,Mysql服務的啟動比Web等慢一些。
PS:經常查看各種服務運行日志是個很好的習慣,也是成為高手的必經之路。
9.開機自啟動
具體命令:
echo "//mysql mutil instances" >>/etc/rc.local echo "/data/3306/mysql start" >>/etc/rc.local echo "/data/3307/mysql start" >>/etc/rc.local
如何增加一個mysql實例
具體命令集合:
mkdir -p /data/3308/data \cp /data/3306/my.cnf /data/3308/ \cp /data/3306/mysql /data/3308/ cd 3308/ sed -i 's/3306/3308/g' /data/3308/my.cnf sed -i 's/server-id = 1/server-id = 8/g' /data/3308/my.cnf sed -i 's/3306/3308/g' /data/3308/mysql chown -R mysql.mysql /data/3308 chmod 700 /data/3308/mysql
#初始化3308 實例Mysql數據庫
[root@db02 ~]# cd /application/mysql/scripts/
#3306實例初始化[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3308/data --user=mysql
#啟動3308實例的數據庫/data/3308/mysql start