Mysql多实例集群搭建
#5.5.x-5.7.x-8.0
mkdir -p /data/{3306,3307}/data
mkdir -p /etc/my.cnf.d
echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
source /etc/profile
#vi /data/3306/my.cnf
#:g/3306/s//3307/g
#sed -i 's/3306/3307' /data/3306/my.cnf
#vi /data/3306/mysql
chown -R mysql:mysql /data/{3306,3307}
#chmod +x /data/{3306,3307}/mysql
#5.5: touch /data/3306/dd.err
#8.0
/usr/local/mysql/bin/mysqld --defaults-file=/data/3306/my.cnf --initialize-insecure --user=mysql
#5.7
/usr/local/mysql/bin/mysqld --defaults-file=/data/3306/my.cnf --initialize-insecure --user=mysql
#5.5
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/data/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
启动/停止实例
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf &
mysqladmin -S /data/3306/mysql.sock shutdown
mysqladmin -uroot -p -S /data/3306/mysql.sock shutdown
ln -s /usr/local/mysql/bin/mysqladmin /usr/sbin/
ln -s /usr/local/mysql/bin/mysqld_safe /usr/sbin/
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
mysql -S /data/3306/mysql.sock
5.5-5.7: mysqladmin -u root -S /data/3306/mysql.sock password '123456'
8.0: alter user 'root'@'localhost' identified by "123456";
mysql -uroot -p -S /data/3306/mysql.sock
mysql -uroot -p –h 192.168.200.98 -P 3306
#5.5多实例主从同步
主从server-id 不同
#主
grant file on *.* to 'dd'@'192.168.%.%' identified by '123456';
or grant all on
grant replication slave on *.* to 'dd'@'192.168.55.138' identified by '123456';
#从
change master to
master_host=''
master_user=''
master_password=''
master_log_file=''
master_log_pos=
--------------------------------------------------------------------------------
#8.0 my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data
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
server-id = 1
binlog_format = mixed
[mysqldump]
quick
max_allowed_packet = 2M
[mysqld_safe]
pid-file=/data/3306/mysqld.pid
--------------------------------------------------------------------------------
#5.7.x my.cnf muti启动
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
port = 3306
socket = /data/3306/mysql.sock
datadir = /data/3306/data
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/data/3306/mysql-bin
binlog_format=mixed
server-id = 3306
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /data/3306
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/3306
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
##########################################
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld1]
port = 3306
socket = /data/3306/mysql.sock
datadir = /data/3306/data
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=/data/3306/mysql-bin
binlog_format=mixed
server-id = 3306
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /data/3306
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/3306
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
user = mysql
[mysqld2]
改3307
[mysqld3]
改3307
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
--------------------------------------------------------------------------------
cp /usr/local/mysql/support-files/mysqld_multi.server /etc/rc.d/init.d/mysqld_multi
/etc/rc.d/init.d/mysqld_multi start
/etc/rc.d/init.d/mysqld_multi report
/etc/rc.d/init.d/mysqld_multi stop 1-3
or
killall -u mysql
or
kill -9 the-mysql-pid
netstat -tnlp | grep 330
如果需要开启root远程登录权限:
use mysql
update user set host='%' where user='root';
flush privileges;
select user,host,authentication_string from user;
--------------------------------------------------------------------------------
#5.5.x my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
skip-name-resolve
log-bin=mysql-bin
binlog_format=mixed
max_binlog_size = 500M
server-id = 1
[mysqld_safe]
log-error=/data/3306/3306.err
pid-file=/data/3306/mysql.pid
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
--------------------------------------------------------------------------------
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysql]
no-auto-rehash
[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_cache = 614
external-locking = FALSE
max_allowed_packet =8M
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
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
#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
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_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 1
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
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
[mysqld_safe]
pid-file=/data/3306/mysqld.pid
--------------------------------------------------------------------------------
#5.5.x-5.7.x-8.0 mysql
#!/bin/sh
port=3306
mysql_user="root"
mysql_pwd="142857"
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