什麼使多實例呢?mysql
多實例就是在一臺機器上開啓多個不一樣的服務端口(如:3306,3307);sql
運行多個MySQL服務進程,這些進程經過不一樣的socket監聽不一樣的服務端口來提供各自的服務。shell
MySQL多實例共用一個 MySQL 的安裝程序,使用不一樣(也可相同)的 my.cnf 配置文件,啓動程序和數據文件。數據庫
在提供服務時,多實例 MySQL 在邏輯上是各自獨立的,多個實例的自己是根據配置文件對應的設定值,來得到服務器的相關硬件資源多少。vim
安裝配置前的一些配置少不了bash
文件的目錄最好也規矩,這樣才專業服務器
/data/ops/app/mysql-5.6.23/ ###mysql的安裝路徑 mkdir /data/ops/app/mysql-5.6.23/{3306,3307} vim /data/ops/app/mysql-5.6.23/etc/3306.cnf [client] character-set-server = utf8 [mysql] character-set-server = utf8 [mysqld] #bind-address=127.0.0.1 character-set-server = utf8 datadir=/data/ops/app/mysql-5.6.23/3306 socket=/data/ops/app/mysql-5.6.23/3306/3306.sock user=mysql skip-grant-tables symbolic-links=0 skip-name-resolve #init-connect='insert into accesslog.accesslog(id, time, localname, matchname) values(connection_id(), now(), user(),current_user());' slow_query_log = 1 long_query_time = 4 #slow-query-log-file = /data/ops/app/mysql-5.6.23/logs/3306/3306_slow.log log-queries-not-using-indexes = false lower-case-table-names = 1 server-id = 1 max_connections =5000 max_allowed_packet = 16M connect_timeout = 60 log-slave-updates log-bin = 3306-bin expire_logs_days = 7 slave-skip-errors = 1062 #binlog_do_db = back_log = 200 max_connect_errors = 10 table_open_cache = 10240 binlog_cache_size = 1M max_heap_table_size = 64M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 8 thread_concurrency = 8 query_cache_size = 512M query_cache_limit = 2M ft_min_word_len = 4 thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M binlog_format = mixed log-short-format key_buffer_size = 512M read_buffer_size = 16M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M innodb_file_per_table innodb_additional_mem_pool_size = 100M innodb_buffer_pool_size = 1G innodb_data_file_path = ibdata1:12M;ibdata2:12M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 8M innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 slave-skip-errors = 1062 [mysqld_safe] log-error=/data/ops/app/mysql-5.6.23/logs/3306/3306.log pid-file=/data/ops/app/mysql-5.6.23/3306/3306.pid
vim /data/ops/app/mysql-5.6.23/etc/3307.cnf ### 配置文件 [client] character-set-server = utf8 [mysql] character-set-server = utf8 [mysqld] #bind-address=127.0.0.1 character-set-server = utf8 datadir=/data/ops/app/mysql-5.6.23/3307 socket=/data/ops/app/mysql-5.6.23/3307/3307.sock user=mysql skip-grant-tables symbolic-links=0 skip-name-resolve #init-connect='insert into accesslog.accesslog(id, time, localname, matchname) values(connection_id(), now(), user(),current_user());' slow_query_log = 1 long_query_time = 4 slow-query-log-file = /data/ops/app/mysql-5.6.23/logs/3307/3307_slow.log log-queries-not-using-indexes = false lower-case-table-names = 1 server-id = 1 max_connections =5000 max_allowed_packet = 16M connect_timeout = 60 log-slave-updates log-bin = 3307-bin expire_logs_days = 7 slave-skip-errors = 1062 #binlog_do_db = back_log = 200 max_connect_errors = 10 table_open_cache = 10240 binlog_cache_size = 1M max_heap_table_size = 64M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 8 thread_concurrency = 8 query_cache_size = 512M query_cache_limit = 2M ft_min_word_len = 4 thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M binlog_format = mixed log-short-format key_buffer_size = 512M read_buffer_size = 16M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M innodb_file_per_table innodb_additional_mem_pool_size = 100M innodb_buffer_pool_size = 1G innodb_data_file_path = ibdata1:12M;ibdata2:12M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 8M innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 slave-skip-errors = 1062 [mysqld_safe] log-error=/data/ops/app/mysql-5.6.23/logs/3307/3307.log pid-file=/data/ops/app/mysql-5.6.23/3307/3307.pid
###建立初始化腳本以及啓動文件app
vim /data/ops/app/mysql-5.6.23/scripts/init/3306.sh #/bin/bash # 3306.sh can create a 3306 instance # create directories if [ ! -d '/data/ops/app/mysql-5.6.23/3306' ];then mkdir -p '/data/ops/app/mysql-5.6.23/3306'; fi if [ ! -d '/data/ops/app/mysql-5.6.23/logs/3306' ];then mkdir -p '/data/ops/app/mysql-5.6.23/logs/3306'; fi # set env conf="/data/ops/app/mysql-5.6.23/etc/3306.cnf" basedir="/data/ops/app/mysql-5.6.23" datadir="/data/ops/app/mysql-5.6.23/3306" pid="/data/ops/app/mysql-5.6.23/3306/3306.pid" sock="/data/ops/app/mysql-5.6.23/3306/3306.sock" port='3000' user='mysql' # create instance #/data/ops/app/mysql-5.6.23/scripts/mysql_install_db --user=$user --basedir=$basedir --datadir=$datadir # boot instance /data/ops/app/mysql-5.6.23/bin/mysqld_safe --defaults-file=$conf --user=$user --basedir=$basedir --datadir=$datadir --port=$port --pid-file=$pid --socket=$sock &
vim /data/ops/app/mysql-5.6.23/scripts/init/3307.sh #/bin/bash # 3307.sh can create a 3307 instance # create directories if [ ! -d '/data/ops/app/mysql-5.6.23/3307' ];then mkdir -p '/data/ops/app/mysql-5.6.23/3307'; fi if [ ! -d '/data/ops/app/mysql-5.6.23/logs/3307' ];then mkdir -p '/data/ops/app/mysql-5.6.23/logs/3307'; fi # set env conf="/data/ops/app/mysql-5.6.23/etc/3307.cnf" basedir="/data/ops/app/mysql-5.6.23" datadir="/data/ops/app/mysql-5.6.23/3307" pid="/data/ops/app/mysql-5.6.23/3307/3307.pid" sock="/data/ops/app/mysql-5.6.23/3307/3307.sock" port='3002' user='mysql' # create instance #/data/ops/app/mysql-5.6.23/scripts/mysql_install_db --user=$user --basedir=$basedir --datadir=$datadir # boot instance /data/ops/app/mysql-5.6.23/bin/mysqld_safe --defaults-file=$conf --user=$user --basedir=$basedir --datadir=$datadir --port=$port --pid-file=$pid --socket=$sock &
#####注意初始化腳本完畢之後mysql才能啓動socket
su --shell=/bin/bash mysql -c "/data/ops/app/mysql-5.6.23/scripts/init/3306.sh" ###啓動腳本 能夠放在/etc/rc.local下 開機自啓orm
su --shell=/bin/bash mysql -c "/data/ops/app/mysql-5.6.23/scripts/init/3307.sh"
####mysql關閉
killall mysqld 不建議這樣操做
mysqladmin -u mysql -P 3002 -S /data/ops/app/mysql-5.6.23/3307/3307.sock shutdown ###建議這樣關閉數據庫
問題又來了,要增長一個實例怎麼弄呢?
其實不難,只需按上面步驟,建立目錄,在修改相應配置文件,初始化就ok