1. 背景
MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的datadir, port, socket, pid都是不同的。
2. 多实例特点
* 有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。
* 资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降。
上图很形象的描述了多实例,一台服务器部署多个mysql数据库,多个mysql数据库共享系统以及硬件资源等。
1、创建多实例数据目录
mkdir -p /data/mysql3306/data
mkdir -p /data/mysql3307/data
2、配置多实例my.conf文件
cd /data/mysql3306/
-----------------------------------------------------------------------
vi my3306.cnf
[client]
port = 3306
socket = /data/mysql3306/mysql3306.sock
[mysql]
auto-rehash
[mysqld_safe]
log-error = /data/mysql3306/my3306.err
pid-error = /data/mysql3306/my3306.pid
[mysqld]
user = mysql
port = 3306
socket= /data/mysql3306/mysql3306.sock
pid-file = /data/mysql3306/mysql3306.pid
datadir = /data/mysql3306/data
basedir = /usr/local/mysql
skip_name_resolve = 1
skip-external-locking
max_connections = 3000
max_connect_errors = 10
transaction_isolation = READ-COMMITTED
interactive_timeout=86400
wait_timeout=86400
back_log=600
####cache######
table_open_cache=2000
thread_cache_size=500
query_cache_size=128M
query_cache_min_res_unit=128k
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=4M
join_buffer_size=2M
bulk_insert_buffer_size=16M
max_heap_table_size=64M
tmp_table_size=64M
###MyISAM####
key_buffer_size=64M
key_cache_block_size=4k
myisam_sort_buffer_size=2M
########innodb settings########
#innodb_page_size = 4k
innodb_buffer_pool_size = 1G
innodb_buffer_pool_instances = 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
#innodb_lru_scan_depth = 2000
innodb_lock_wait_timeout = 50
#innodb_io_capacity = 4000
#innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_log_group_home_dir = /data/mysql3306/redolog/
innodb_undo_directory = /data/mysql3306/undolog/
innodb_undo_logs = 128
#innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 4M
######mysqld-5.7########
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
show_compatibility_56=on
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#skip-grant-tables
--------------------------------------------------------------------------------------------------------------
cd /data/mysql3307/
vi my3307.cnf
[client]
port = 3307
socket = /data/mysql3307/mysql3307.sock
[mysql]
auto-rehash
[mysqld]
user = mysql
port = 3307
socket= /data/mysql3307/mysql3307.sock
pid-file = /data/mysql3307/mysql3307.pid
datadir = /data/mysql3307/data
basedir = /usr/local/mysql
log-error = /data/mysql3307/my3307.err
skip_name_resolve = 1
skip-external-locking
max_connections = 3000
max_connect_errors = 10
transaction_isolation = READ-COMMITTED
interactive_timeout=86400
wait_timeout=86400
back_log=600
####cache######
table_open_cache=2000
thread_cache_size=500
query_cache_size=128M
query_cache_min_res_unit=128k
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=4M
join_buffer_size=2M
bulk_insert_buffer_size=16M
max_heap_table_size=64M
tmp_table_size=64M
###MyISAM####
key_buffer_size=64M
key_cache_block_size=4k
myisam_sort_buffer_size=2M
########innodb settings########
#innodb_page_size = 4k
innodb_buffer_pool_size = 1G
innodb_buffer_pool_instances = 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
#innodb_lru_scan_depth = 2000
innodb_lock_wait_timeout = 50
#innodb_io_capacity = 4000
#innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_log_group_home_dir = /data/mysql3307/redolog/
innodb_undo_directory = /data/mysql3307/undolog/
innodb_undo_logs = 128
#innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 4M
######mysqld-5.7########
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
show_compatibility_56=on
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#skip-grant-tables
--------------------------------------------------------------------------------------------------------------
3、初始化mysql多实例
需要说明的是,在这之前你已经源码编译或其他方式安装过mysql,安装方式参考:
http://blog.csdn.net/sj349781478/article/details/76994990
接着说初始化
chown -R mysql:mysql /data
cd /usr/local/mysql/bin
./mysqld --defaults-file=/data/mysql3306/my3306.cnf --user=mysql --basedir=/usr/local/mysql --initialize-insecure
./mysqld --defaults-file=/data/mysql3307/my3307.cnf --user=mysql --basedir=/usr/local/mysql --initialize-insecure
启用mysql实例
./mysqld_safe --defaults-file=/data/mysql3306/my3306.cnf &
./mysqld_safe --defaults-file=/data/mysql3307/my3307.cnf &
查看是否启动
ps -ef | grep mysql
设置密码
mysqladmin -uroot -p password 123456 -S /data/mysql3306/mysql3306.sock
本地登录
mysql -uroot -p -S /data/mysql3306/mysql3306.sock
关闭某个实例
cd /usr/local/mysql/bin
./mysqladmin -uroot -p -S /data/mysql3306/mysql3306.sock shutdown
------------------------------------------------------------------------------------------------------------------------------------
4、继续添加实例3308
cd /data/
mkdir mysql3308
cd mysql3308
mkdir data
mkdir redolog
mkdir undolog
cp /data/mysql3306/my3306.cnf .
mv my3306.cnf my3308.cnf
vi my3308.cnf
:g/3306/s//3308/g
wq
chown -R mysql:mysql /data
cd /usr/local/mysql/bin
./mysqld --defaults-file=/data/mysql3309/my3309.cnf --user=mysql --basedir=/usr/local/mysql --initialize-insecure
./mysqld_safe --defaults-file=/data/mysql3309/my3309.cnf &
最后想给出一个规范的my.cnf
#
[client]
port = 3306
socket = /tmpl/mysql.sock
[mysql]
prompt="\u@jsshapp \R:\m:\s [\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data/
socket = /tmpl/mysql.sock
pid-file = jsshapp.pid
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 100
max_connect_errors = 1000000
table_open_cache = 100
table_definition_cache = 100
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 150
query_cache_size = 0
query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /data/mysql/mysql3306/data/slow.log
log-error = /data/mysql/mysql3306/data/error.log
long_query_time = 0.1
server-id = 3306
log-bin = /data/mysql/mybinlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
expire_logs_days = 28
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 15M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 4014M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
innodb_status_file = 1
innodb_status_output = 0
innodb_status_output_locks = 0
#performance_schema
performance_schema = 1
performance_schema_instrument = '%=on'
#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
[mysqldump]
quick
max_allowed_packet = 32M
参考文章:
http://www.cnblogs.com/wajika/p/6242085.html
http://www.linuxidc.com/Linux/2017-07/145343.htm
http://blog.csdn.net/sz_bdqn/article/details/46092375