在一台服务器上开启多个不同的服务端口,运行多个MySQL服务进程,这些服务端口通过不同的socket监听不同的服务端口来提供各自的服务
共用一套MySQL安装程序,使用不同的配置文件,启动程序和数据文件;但是他们公用系统资源,比如CPU,内存,磁盘等,所以逻辑上独立,但是彼此还是有一定的影响,比如一个实例查询量很大,其他进程多少会受到一些影响。
作用:
有效利用服务器资源
节约服务器资源
应用场景:
资金紧张;并发访问并不是很大的业务
一般内存24G-32G,8核CPU,磁盘 6*600Gsas 15k,可以跑2-3实例
1多个配置文件 多个启动程序
2musql-multi
我们使用方案一安装
ncurses-devellibaio-devel
pkillmysqld
rm-f /etc/init.d/mysqld
mkdir-p /data/{3306,3307}/data
[client]
#password = your_password
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
skip-external-locking
#key_buffer_size = 16M
key_buffer_size= 16K
max_allowed_packet= 1M
table_open_cache= 64
sort_buffer_size= 512K
read_buffer_size= 1M
read_rnd_buffer_size= 1M
net_buffer_length= 8K
thread_stack= 128K
log-error=/data/3306/error.log
log-slow-queries=/data/3306/slow.log
pid-file=/data/3306/mysql.pid
log-bin=/data/3306/mysql-bin
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
expire_logs_days=7
bulk_insert_buffer_size=1M
myisam_sort_buffer_size=1M
myisam_max_sort_file_size=10M
myisam_repair_threads=1
myisam_recover
lower_case_table_names=1
skip-name-resolve
slave-skip-errors=1032,1062
replicate-ignore-db=mysql
#Don't listen on a TCP/IP port at all. This can be a security enhancement,
#if all processes that need to connect to mysqld run on the same host.
#All interaction with mysqld must be made via Unix sockets or named pipes.
#Note that using this option without enabling named pipes on Windows
#(using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id = 1
#Uncomment the following if you want to log updates
#log-bin=mysql-bin
#binary logging format - mixed recommended
#binlog_format=mixed
#Causes updates to non-transactional engines using statement format to be
#written directly to binary log. Before using this option make sure that
#there are no dependencies between transactional and non-transactional
#tables such as in the statement INSERT INTO t_myisam SELECT * FROM
#t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE
#Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir= /usr/local/mysql/data
#8innodb_data_file_path = ibdata1:128M:autoextend
innodb_data_file_path= ibdata1:10M:autoextend
#innodb_log_group_home_dir= /usr/local/mysql/data
#You can set .._buffer_pool_size up to 50 - 80 %
#of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 32M
innodb_buffer_pool_size= 16M
innodb_file_io_threads=4
innodb_thread_concurrency=8
innodb_additional_mem_pool_size= 4M
#Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size= 5M
innodb_log_file_size= 4M
#innodb_log_buffer_size= 8M
innodb_log_buffer_size= 2M
#innodb_flush_log_at_trx_commit= 1
innodb_flush_log_at_trx_commit=2
#innodb_lock_wait_timeout= 50
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= 16M
max_allowed_packet= 2M
[mysql]
no-auto-rehash
#Remove the next comment character if you are not familiar with SQL
#safe-updates
[mysqld_safe]
log-error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysqld.pid
[myisamchk]
key_buffer_size= 8M
sort_buffer_size= 8M
[mysqlhotcopy]
interactive-timeout
同理配置/data/3307/my.cnf,我们只需要改一下端口
#Example MySQL config file for small systems.
#
#This is for a system with little memory (<= 64M) where MySQL is only used
#from time to time and it's important that the mysqld daemon
#doesn't use much resources.
#
#MySQL programs look for option files in a set of
#locations which depend on the deployment platform.
#You can copy this option file to one of those
#locations. For information about these locations, see:
#http://dev.mysql.com/doc/mysql/en/option-files.html
#
#In this file, you can use all long options that a program supports.
#If you want to know which options a program supports, run the program
#with the "--help" option.
#The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3307
socket = /data/3307/mysql.sock
#Here follows entries for some specific program
#The MySQL server
[mysqld]
user= mysql
port = 3307
socket = /data/3307/mysql.sock
basedir=/usr/local/mysql
datadir=/data/3307/data
open_files_limit=1024
back_log=600
skip-external-locking
#key_buffer_size = 16M
key_buffer_size= 16K
max_allowed_packet= 1M
table_open_cache= 64
sort_buffer_size= 512K
read_buffer_size= 1M
read_rnd_buffer_size= 1M
net_buffer_length= 8K
thread_stack= 128K
log-error=/data/3307/error.log
log-slow-queries=/data/3307/slow.log
pid-file=/data/3307/mysql.pid
#log-bin=/data/3307/mysql-bin
relay-log=/data/3307/relay-bin
relay-log-info-file=/data/3307/relay-log.info
binlog_cache_size=1M
max_binlog_cache_size=1M
max_binlog_size=2M
expire_logs_days=7
bulk_insert_buffer_size=1M
myisam_sort_buffer_size=1M
myisam_max_sort_file_size=10M
myisam_repair_threads=1
myisam_recover
lower_case_table_names=1
skip-name-resolve
slave-skip-errors=1032,1062
replicate-ignore-db=mysql
#Don't listen on a TCP/IP port at all. This can be a security enhancement,
#if all processes that need to connect to mysqld run on the same host.
#All interaction with mysqld must be made via Unix sockets or named pipes.
#Note that using this option without enabling named pipes on Windows
#(using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id = 3
#Uncomment the following if you want to log updates
#log-bin=mysql-bin
#binary logging format - mixed recommended
#binlog_format=mixed
#Causes updates to non-transactional engines using statement format to be
#written directly to binary log. Before using this option make sure that
#there are no dependencies between transactional and non-transactional
#tables such as in the statement INSERT INTO t_myisam SELECT * FROM
#t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE
#Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir= /usr/local/mysql/data
#8innodb_data_file_path = ibdata1:128M:autoextend
innodb_data_file_path= ibdata1:10M:autoextend
#innodb_log_group_home_dir= /usr/local/mysql/data
#You can set .._buffer_pool_size up to 50 - 80 %
#of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 32M
innodb_buffer_pool_size= 16M
innodb_file_io_threads=4
innodb_thread_concurrency=8
innodb_additional_mem_pool_size= 4M
#Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size= 5M
innodb_log_file_size= 4M
#innodb_log_buffer_size= 8M
innodb_log_buffer_size= 2M
#innodb_flush_log_at_trx_commit= 1
innodb_flush_log_at_trx_commit=2
#innodb_lock_wait_timeout= 50
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= 16M
max_allowed_packet= 2M
[mysql]
no-auto-rehash
#Remove the next comment character if you are not familiar with SQL
#safe-updates
[mysqld_safe]
log-error=/data/3307/mysql_3307.err
pid-file=/data/3307/mysqld.pid
[myisamchk]
key_buffer_size= 8M
sort_buffer_size= 8M
[mysqlhotcopy]
interactive-timeout
为什么初始化?
初始化就是创建基础的数据库文件,列入生成mysql表等
./mysql_install_db--basedir=/usr/local/mysql/ --datadir=/data/3306/data/ --user=mysql
./mysql_install_db--basedir=/usr/local/mysql/ --datadir=/data/3307/data/ --user=mysql
注意:必须指定--user参数,否则如果你用用root用户下,那么生成文件全是root权限的,mysql用户登录会有问题
第六步:多实例的启动
如果my.cnf不在/etc下面,我们在启动mysql需要指定my.cnf的位置比如:
mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
mysqldadmin–u${username} –p${password} -S ${sock-file} shutdown
mysqldadmin-uroot -p123456 -S /data/3306/mysql.sock shutdown
mysqldadmin-uroot -p123456 -S /data/3307/mysql.sock shutdown
编写启动脚本
#!/bin/bash
#init
MYSQL_HOME=/usr/local/mysql
PORT=3306
USERNAME="root"
PASSWORD="123456"
MYSQL_SOCK=/data/$PORT/mysql.sock
MYSQL_CONFIG=/data/$PORT/my.cnf
#startfucntion
function_start_mysql(){
if [ ! -e "$MYSQL_SOCK" ]; then
printf "StartingMySQL...\n"
/bin/sh $MYSQL_HOME/bin/mysqld_safe--defaults-file=$MYSQL_CONFIG 2>&1 > /dev/null &
else
printf "MySQL isrunning...\n"
exit
fi
}
#stop
function_stop_mysql(){
if [ ! -e "$MYSQL_SOCK" ]; then
printf "MySQL isstopped...\n"
exit
else
printf "StoppingMySQL...\n"
/bin/sh $MYSQL_HOME/bin/mysqldadmin-u$USERNAME -p$PASSWORD -S $MYSQL_SOCK shutdown
fi
}
#restart
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
/opt/shell/mysql/3306/mysql.shstart
/opt/shell/mysql/3307/mysql.shstart
这时候我们可以以root用户登录,也可以用匿名用户登录,但是是有问题的,为什么呢,我们没有为root用户设置密码,默认是需要为root用户设置密码。
mysql-uroot -p123456 -S /data/3306/mysql.sock
ERROR2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'(2)
方案:
1以安全模式进入到MySQL
/usr/local/mysql/bin/mysqld_safe--defaults-file=/data/3306/my.cnf --skip-grant-tables --skip-networking &
2重新开一个窗口,然后mysql-uroot –S /data/3306/mysql
usemysql;
UPDATEuser SET password='123456' WHERE user='root';
3刷新权限表
flush privileges;
4退出mysql,关闭安全模式
5然后正常启动mysqld,/opt/shell/mysql/3306/mysql.shstart
6然后在登录mysql
mysql-uroot -p 123456 -S /data/3306/mysql
7发现还是有问题:
ERROR1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
8那应该是我们没有给root用户赋予权限
9然后再停掉mysql,再以安全模式,进入mysql
10设置权限
usemysql;
GRANTALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
这时候会有一个错误,说在skip-grant-tables模式下,不能执行此操作。
没关系,我们刷新一下权限表
flush privileges;
再次执行:GRANTALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
再刷新一系权限表。
11退出安全模式,重新正常启动mysql,然后登陆
mysql-uroot -p 123456 -S /data/3306/mysql
然后就可以了。
然后3307也是做同样的操作。