Step 1 . 准备工作
关闭防火墙; 安装 MySQL依赖于libaio 库;下载,解压,重命名 MySQL的执行文件;新建用户组和用户 等准备工作在此不再赘述。
可执行文件目录为 /data/mysql57
添加用户和组的指令
groupadd mysql
useradd mysql -g mysql
Step 2 . 添加环境变量
在/etc/profile文件中,追加 PATH=${PATH}:/data/mysql57/bin/
保存后,如需要立即生效,可执行 source /etc/profile 。
step 3 . 创建目录并授权
在data根目录下执行
mkdir -p mysql3306/data mysql3306/mysql_log mysql3306/tmp mysql3307/data mysql3307/mysql_log mysql3307/tmp mysqld_multi/log
创建记录log的文件,此步骤不可少。
touch /data/mysql3306/mysql_log/mysql3306.err touch /data/mysql3307/mysql_log/mysql3307.err
赋予目录和文件权限
chown -R mysql.mysql mysql3* mysqld_multi
在本测试案例中,mysql57是从其它服务上copy来的,不是直接下载解压的,所以多了下面的两步授权操作。
chmod -R /data/mysql57/bin
chmod -R /data/mysql57/support-files
step 4 . 编辑my.cnf
[client]
host=localhost
socket = /tmp/mysql.sock
default-character-set=utf8mb4
#loose-local-infile= [mysqld]
user=mysql
log_bin_trust_function_creators=
secure_file_priv='/tmp' ########server setting#######
sql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
character-set-server=utf8mb4
collation_server=utf8mb4_unicode_ci
lower_case_table_names =
skip_name_resolve =
#max_connect_errors =
max_connections =
thread_cache_size=
#thread_stack=
#back_log=
max_allowed_packet =
event_scheduler =
local-infile=
#lower_case_table_names =
explicit_defaults_for_timestamp =
expire_logs_days =
log_bin_trust_function_creators = ####.frm/.ibd files qty related
open_files_limit=
innodb_open_files=
table_open_cache=
table_definition_cache= #### seesion buffer related
read_buffer_size =
read_rnd_buffer_size =
sort_buffer_size =
join_buffer_size = ####memory table size
tmp_table_size =
max_heap_table_size= ####timeout
interactive_timeout =
wait_timeout =
# connect_timeout= ########slow query ########
slow_query_log =
log_slow_slave_statements =
#log_queries_not_using_indexes =
log_throttle_queries_not_using_indexes =
long_query_time =
#min_examined_row_limit = ########innodb settings########
innodb_buffer_pool_size =
innodb_buffer_pool_instances =
innodb_buffer_pool_dump_pct =
innodb_lru_scan_depth =
innodb_page_cleaners =
#innodb_purge_threads =
innodb_sort_buffer_size =
#innodb_file_per_table =
#innodb_flush_log_at_trx_commit = innodb_undo_log_truncate =
innodb_undo_tablespaces =
innodb_max_undo_log_size =
innodb_purge_rseg_truncate_frequency = innodb_log_file_size =
innodb_log_files_in_group =
innodb_log_buffer_size =
innodb_flush_method = O_DIRECT
innodb_flush_neighbors =
innodb_print_all_deadlocks = innodb_strict_mode =
#innodb_lock_wait_timeout = innodb_io_capacity =
innodb_io_capacity_max =
innodb_thread_concurrency =
innodb_write_io_threads =
innodb_read_io_threads = ########replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE gtid_mode = on
enforce_gtid_consistency =
binlog_gtid_simple_recovery=
relay_log_recovery = slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers =
slave_transaction_retries=
slave_preserve_commit_order= log_slave_updates=
binlog_format = ROW
log_timestamps=system binlog_rows_query_log_events =
binlog_row_image='full'
slave_skip_errors = ddl_exist_errors ########semi sync replication settings########
##plugin_dir=/data/mysql/plugin/
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
#rpl_semi_sync_master_enabled =
#rpl_semi_sync_master_timeout =
#rpl_semi_sync_slave_enabled = [mysqld_multi]
mysqld = /data/mysql57/bin/mysqld_safe
mysqladmin = /data/mysql57/bin/mysqladmin
log = /data/mysqld_multi/log/mysqld_multi.log [mysqld3306]
basedir = /data/mysql57
mysqladmin=mysqladmin
datadir=/data/mysql3306/data
port=
server_id=
socket= /tmp/mysql_3306.sock
tmpdir = /data/mysql3306/tmp
pid-file = /data/mysql3306/mysql_log/mysql3306.pid
slow_query_log_file = /data/mysql3306/mysql_log/mysql3306_slow_new.log
log-error = /data/mysql3306/mysql_log/mysql3306.err
general_log_file= /data/mysql3306/mysql_log/mysql3306.genlog
log-bin = /data/mysql3306/mysql_log/mysql3306_bin
relay_log = /data/mysql3306/mysql_log/relay3306.log innodb_buffer_pool_size = 90G
innodb_buffer_pool_instances = [mysqld3307]
basedir = /data/mysql57
mysqladmin=mysqladmin
datadir=/data/mysql3307/data
port=
server_id=
socket= /tmp/mysql_3307.sock
tmpdir = /data/mysql3307/tmp
pid-file = /data/mysql3307/mysql_log/mysql3307.pid
slow_query_log_file = /data/mysql3307/mysql_log/mysql3307_slow_new.log
log-error = /data/mysql3307/mysql_log/mysql3307.err
general_log_file= /data/mysql3307/mysql_log/mysql3307.genlog
log-bin = /data/mysql3307/mysql_log/mysql3307_bin
relay_log = /data/mysql3307/mysql_log/relay3307.log innodb_buffer_pool_size = 90G
innodb_buffer_pool_instances = [mysqldump]
quick
step 5 . 初始化实例
初始化3306端口的实例,注意产生的临时密码。
/data/mysql57/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/data/mysql57 --datadir=/data/mysql3306/data
初始化3307端口的实例,注意产生的临时密码。
/data/mysql57/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/data/mysql57 --datadir=/data/mysql3307/data
step 6 . 复制生成mysqld_multi并添加到开机启动
cp /data/mysql57/support-files/mysqld_multi.server /etc/init.d/mysqld_multi
chkconfig --add mysqld_multi
step 7 . 多实例Mysqld的开启
开启全部实例
mysqld_multi start
查看开启情况(查看全部实例状态)
mysqld_multi report
开启指定实例
开启/etc/my.cnf中[mysqld3306]其中mysqld后面的数字为标签,例如3306标签
mysqld_multi start
同样开启3307标签实例
mysqld_multi start
(注意:此处没有说通过 mysqld_multi stop 命令进行关闭实例,为什么不说,因为命令无效。Step 9 的操作会让它变成有效)
step 8 . 第一次登入实例,修改root账号密码
多实例登入需指定 socket 参数
本测试3306实例登入的方式为:
mysql -S /tmp/mysql_3306.sock -uroot --port -p
step 9 . 赋予通过mysqld_multi stop关闭实例的权限。
关闭实例需要配置root用户及密码, 修改 /etc/my.cnf文件。
在 [client] 位置添加
user=root
password=密码
因为文件中需要保留账号密码,有安全风险。实际环境中要不要如此设置,还需根据具体情况和安全要求而定。
经过以上步骤的操作,此服务器成功安装了2个MySQL实例,一个Port为3306,另一个Port为3307
感谢:以上文档在同事Fly Chen的基础梳理完成。