mysql多实例就是在一台服务器上同时开启多个不同的服务端口,同时运行多个mysql,这些服务进程通过不同的socket监听不同的服务器端口来提供服务.
这些MySQL多实例共用一套MySQL安装程序,使用不同的my.cnf(也可以相同)配置文件,启动程序(也可以相同)和数据文件。在提供服务时,多实例MySQL在逻辑上看起来是各自独立的,它们根据配置文件的对应设定值,获得服务器相应数量的硬件资源。
MySQL多实例的作用:(1)有效利用服务器资源 (2)节约服务器资源
配置
创建mysql多实例数据目录
mkdir -p /data/{3306,3307}/data
tree /data/ /data/ ├── 3306 #3306实例目录 │ └── data #3306实例的数据文件目录 ├── 3307 #3307实例目录 └── data #3307实例的数据文件目录
创建Mysql多实例的配置文件
vim /data/3306/my.cnf
模板如下:
[client] port = 3306 socket = /data/3306/mysql.sock [mysqld] user = mysql port = 3306 socket = /data/3306/mysql.sock basedir = /usr/local/mysql datadir = /data/3306/data open_files_limit = 1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_open_cache = 614 external-locking = FALSE max_allowed_packet = 8M #binlog_cache_size = 1M #max_heap_table_size = 64M #read_buffer_size = 2M #read_rnd_buffer_size = 16M 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 #ft_min_word_len = 4 #default-storage-engine = MYISAM thread_stack = 192K transaction_isolation = READ-COMMITTED tmp_table_size = 2M max_heap_table_size = 2M #log-bin=mysql-bin #binlog_format=mixed #slow_query_log long_query_time = 1 pid-file = /data/3306/mysql.pid 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 key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062 replicate-ignore-db = mysql server-id = 1 #key_buffer_size = 32M #bulk_insert_buffer_size = 64M #myisam_sort_buffer_size = 128M #myisam_max_sort_file_size = 10G #myisam_repair_threads = 1 #myisam_recover innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 32M innodb_data_file_path = ibdata1:128M:autoextend innodb_file_io_threads = 4 #innodb_write_io_threads = 8 #innodb_read_io_threads = 8 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 [mysql] no-auto-rehash #[myisamchk] #key_buffer_size = 512M #sort_buffer_size = 512M #read_buffer = 8M #write_buffer = 8M #[mysqlhotcopy] #interactive-timeout [mysqld_safe] log-error = /data/3306/mysql_wk306.err pid-file = /data/3306/mysqld.pid
3307与3306的区别 将文件里的3306换为3307即可
cp /data/3306/my.cnf /data/3307/ cd /data/3307 vim my.cnf % s#3306#3307#g #替换全局3306为3307
server-id = 1 #server-id不能相同
创建多实例启动文件
vim /data/3306/mysql
模板如下:
#!/bin/bash ############################################### #this scripts is created by wk at 2016-06-25 port=3306 mysql_user="root" mysql_pwd="" #这里需要修改为用户的实际密码 CmdPath="/usr/local/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实例的启动文件只需修改3306启动文件的端口即可
cp /data/3306/mysql /data/3307/ cd /data/3307 vim mysql
配置MySQL多实例的文件权限
chown -R mysql.mysql /data/ #设置属主属组 find /data -name "mysql" | xargs chmod 700 #将不同目录的两个mysql文件设为700权限
MySQL相关命令加入全局路径的配置
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile #添加全局变量 source /etc/profile #使其立即生效
初始化MySQL多实例的数据库文件
cd /usr/local/mysql/scripts/ ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/3306/data --user=mysql #初始化3306数据库 ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/3307/data --user=mysql #初始化3307数据库 #--basedir=/usr/local/mysql为MySQL的安装路径,--datadir为不同的实例数据目录
启动MySQL多实例的命令
/data/3306/mysql start #启动3306 /data/3307/mysql start #启动3307 netstat -antup | grep 330 #查看是否启动
登陆MySQL
mysql -S /data/3306/mysql.sock mysql -S /data/3307/mysql.sock
重启对应的库
/data/3306/mysql stop /data/3307/mysql start
MySQL安全配置
设置登录密码:
mysqladmin -u root -S /data/3306/mysql.sock password '123123' #为mysql设置密码
mysql启动脚本里有mysql 密码设定的变量 修改后的密码必须在启动脚本里也进行修改.两者一样
禁止使用pkill,kill -9,killall -9等命令强制杀死数据库,这会引起数据库无法启动等故障的发生
再增加一个MySQL的实例
只需要在/data下创建3308
mkdir -p /data/3308/data/
导入配置文件my.cnf #参照3307导入
启动脚本mysql #参照3307导入
设置属主属组
chown -R mysql.mysql /datac
给与权限
chmod 700 mysql
初始化配置文件
cd /usr/local/mysql/scripts/ ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/3308/data --user=mysql
执行启动脚本即可执行