mysql多实例
一般情况下是1-4个实例,1-2个居多
一、安装环境
二、多实例添加(安装mysql)
三、配置文件的添加
四、启动脚本的添加
五、文件权限及所属
六、配置全局路径
七、初始化数据库
八、mysql启动、关闭、登录
一、安装环境
CentOS Linux release 7.2.1511
安装目录:/application/mysql
安装版本:mysql-5.7.9
二、实例的添加(安装mysql)
见博文mysql安装 https://blog.51cto.com/13006632/2457769
mkdir /data/{3306,3307}/data -p
三、配置文件my.cnf的添加
[client]port = 3306 #实例端口,另一个为3307socket = /data/3306/mysql.sock[mysql]no-auto-rehash[mysqld]user = mysqlport = 3306socket =/data/3306/mysql.sockbasedir = /application/mysqldatadir = /data/3306/dataopen_files_limit = 1024back_log = 600max_connections = 800max_connect_errors = 3000table_open_cache = 614external-locking = FALSEmax_allowed_packet = 8Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 100query_cache_size = 2Mquery_cache_limit = 1M#default_table_type = InnoDBthread_stack =192k#transaction_isolation = READ-COMMITTEDtmp_table_size = 2Mmax_heap_table_size = 2Mlong_query_time = 1#log_long_format#log-error = /data/3306/error.log#log-slow-queries = /data/3306/slow.logpid-file = /data/3306/mysql.pidlog-bin = /data/3306/mysql-binrelay-log =/data/3306/relay-binrelay-log-info-file = /data/3306/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_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_extra_sort_file_size = 10G #uyisam_repair_threads = 1 #yisam_recoverlower_case_table_names = 1skip-name-resolveslave-skip-errors = 1032,1062replicate-ignore-db = mysqlserver-id = 8 innodb_buffer_pool_size = 32Minnodb_data_file_path = ibdata1:128M:autoextend innodb_thread_concurrency =8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4Minnodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump]quickmax_allowed_packet = 2M[mysqld_safe]log-error = /data/3306/mysql_3306.err pid-file = /data/3306/mysqld.pid
四、mysql脚本的添加
#!/bin/sh#initport=3306mysql_user="root"mysql_pwd="123456"CmdPath="/application/mysql/bin"mysql_sock="/data/${port}/mysql.sock"#startup functionfunction_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 functionfunction_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 functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql}case $1 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /data/${port}/mysql {start|stop|restart}\n"esac
特别说明:来源于老男孩教育
启动原理
/bin/sh /application/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf
停止原理
/application/mysq/bin/mysqladmin -uroot -poldboy -S /data/3306/mysql.sock shutdown
将my.cnf修改为3306和3307后和mysql脚本,放在/data/3306和/data/3307下
[root@mysql data]# tree.├── 3306│ ├── data│ ├── my.cnf│ └── mysql└── 3307 ├── data ├── my.cnf └── mysql4 directories, 4 files
五、文件权限及所属
chown -R mysql.mysql /datafind /data -type f -name "mysql" |xargs chmod 700find /data -type f -name "mysql" |xargs ls -l
六、全局路径配置
echo 'export PATH=/application/mysq/bin:$PATH' >>/etc/profiletail -1 /etc/profilesource /etc/profile
七、初始化数据库
cd /application/mysql/./bin/mysqld --defaults-file=/data/3306/my.cnf --initialize --basedir=/application/mysql/ --datadir=/data/3306/data/ --user=mysql./bin/mysqld --defaults-file=/data/3307/my.cnf --initialize --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
八、mysql启动、关闭、登录
/data/3307/mysql start/data/3307/mysql stop#mysql登录mysql -S /data/3307/mysql.sock #修改密码mysqladmin -uroot -p'password' password '123456' -S /data/3307/mysql.sock#远程登录多实例mysql -uuser -p'password' -h292.168.1.23 -P 3307
九、报错处理
报错1:
启动不起来,日志显示:2019-12-12T17:51:06.643562Z 0 [ERROR] InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 768 pages (rounded down to MB) than specified in the .cnf file: initial 8192 pages, max 0 (relevant if non-zero) pages!2019-12-12T17:51:06.643597Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error2019-12-12T17:51:06.945987Z 0 [ERROR] Plugin 'InnoDB' init function returned error.2019-12-12T17:51:06.946185Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.2019-12-12T17:51:06.946203Z 0 [ERROR] Failed to initialize plugins.2019-12-12T17:51:06.946214Z 0 [ERROR] Aborting
解决方法:
初始化没有加配置文件重新初始化
./bin/mysqld --initialize --defaults-file=/data/3307/my.cnf --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
参数顺序有问题如下:报错2
报错2:
2019-12-12T18:24:10.667460Z 0 [ERROR] unknown variable 'defaults-file=/data/3307/my.cnf'2019-12-12T18:24:10.667497Z 0 [ERROR] Aborting
原因见博客:https://www.cnblogs.com/jerry-rock/p/7380922.html
解决方法,修改成如下:
./bin/mysqld --defaults-file=/data/3307/my.cnf --initialize --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
报错3:
2019-12-12T18:22:50.814935Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.2019-12-12T18:22:50.814979Z 0 [ERROR] Aborting
原因:/data/3307/data/ 目录有旧的初始化文件需要删除清空。
解决方法:
rm -fr /data/3307/data/*
报错4:
2019-12-12T18:29:32.841614Z 0 [ERROR] unknown variable 'table_cache=64' #改为table_open_cache 2019-12-12T18:29:32.841644Z 0 [ERROR] unknown variable 'thread_concurrency=2' #需删除 2019-12-12T19:04:17.320405Z 0 [ERROR] unknown variable 'innodb_additional_mem_pool_size=4M' #需删除2019-12-12T19:14:21.886638Z 0 [ERROR] unknown variable 'innodb_file_io_threads=4' #需删除2019-12-12T18:29:32.841644Z 0 [ERROR] Aborting
原因见博客:https://www.cnblogs.com/putihk/p/7967829.html
mysql版本5.7,我用的my.cnf配置文件中参数名字改动或删减