溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL中多實(shí)例配置和管理的示例分析

發(fā)布時(shí)間:2021-06-08 11:03:48 來(lái)源:億速云 閱讀:93 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章主要介紹MySQL中多實(shí)例配置和管理的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

mysql的多實(shí)例有兩種方式可以實(shí)現(xiàn),兩種方式各有利弊。

  • 第一種是使用多個(gè)配置文件啟動(dòng)不同的進(jìn)程來(lái)實(shí)現(xiàn)多實(shí)例,這種方式的優(yōu)勢(shì)邏輯簡(jiǎn)單,配置簡(jiǎn)單,缺點(diǎn)是管理起來(lái)不太方便。

  • 第二種是通過(guò)官方自帶的mysqld_multi使用單獨(dú)的配置文件來(lái)實(shí)現(xiàn)多實(shí)例,這種方式定制每個(gè)實(shí)例的配置不太方面,優(yōu)點(diǎn)是管理起來(lái)很方便,集中管理。

下面就分別來(lái)實(shí)戰(zhàn)這兩種多實(shí)例的安裝和管理

先來(lái)學(xué)習(xí)第一種使用多個(gè)配置文件啟動(dòng)多個(gè)不同進(jìn)程的情況:

環(huán)境介紹:

mysql 版本:5.1.50

操作系統(tǒng):SUSE 11

mysql實(shí)例數(shù):3個(gè)

實(shí)例占用端口分別為:3306、3307、3308

創(chuàng)建mysql用戶:

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

編譯安裝mysql:

tar xzvf mysql-5.1.50.tar.gz
cd mysql-5.1.50
./configure '--prefix=/usr/local/mysql' '--with-charset=utf8' '--with-extra-charsets=complex' '--with-pthread' '--enable-thread-safe-client' '--with-ssl' '--with-client-ldflags=-all-static' '--with-mysqld-ldflags=-all-static' '--with-plugins=partition,innobase,blackhole,myisam,innodb_plugin,heap,archive' '--enable-shared' '--enable-assembler'
make
make install

初始化數(shù)據(jù)庫(kù):

/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3306 --user=mysql
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3307 --user=mysql
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3308 --user=mysql

創(chuàng)建配置文件

vim /data/dbdata_3306/my.cnf

3306的配置文件如下:

[client]
port = 3306
socket = /data/dbdata_3306/mysql.sock
[mysqld]
datadir=/data/dbdata_3306/
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
port = 3306
socket = /data/dbdata_3306/mysql.sock
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\u@\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192

vim /data/dbdata_3307/my.cnf

3307的配置文件如下:

[client]
port = 3307
socket = /data/dbdata_3307/mysql.sock
[mysqld]
datadir=/data/dbdata_3307/
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
port = 3307
socket = /data/dbdata_3307/mysql.sock
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\u@\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192

vim /data/dbdata_3308/my.cnf

3308的配置文件如下:

[client]
port = 3308
socket = /data/dbdata_3308/mysql.sock
[mysqld]
datadir=/data/dbdata_3308/
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
port = 3308
socket = /data/dbdata_3308/mysql.sock
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\u@\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192

創(chuàng)建自動(dòng)啟動(dòng)文件

vim /data/dbdata_3306/mysqld

3306的啟動(dòng)文件如下:

#!/bin/bash
mysql_port=3306
mysql_username="admin"
mysql_password="password"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/dbdata_${mysql_port}/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/dbdata_${mysql_port}/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac

vim /data/dbdata_3307/mysqld

3307的啟動(dòng)文件如下:

#!/bin/bash
mysql_port=3307
mysql_username="admin"
mysql_password="password"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/dbdata_${mysql_port}/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/dbdata_${mysql_port}/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac

vim /data/dbdata_3308/mysqld

3308的啟動(dòng)文件如下:

#!/bin/bash
mysql_port=3308
mysql_username="admin"
mysql_password="password"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/dbdata_${mysql_port}/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/dbdata_${mysql_port}/mysql.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac

啟動(dòng)3306、3307、3308的mysql

/data/dbdata_3306/mysqld start
/data/dbdata_3307/mysqld start
/data/dbdata_3308/mysqld start

更改原來(lái)密碼(處于安全考慮,還需要?jiǎng)h除系統(tǒng)中沒有密碼的帳號(hào),這里省略了):

/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3306/mysql.sock
/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3307/mysql.sock
/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3308/mysql.sock

登錄測(cè)試并創(chuàng)建關(guān)閉mysql的帳號(hào)權(quán)限,mysqld腳本要用到!

/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;
/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;
/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;

創(chuàng)建了admin帳號(hào)以后腳本的stop功能和restart功能就正常了!

更改環(huán)境變量

vim /etc/profile 添加下面一行內(nèi)容
PATH=${PATH}:/usr/local/mysql/bin/
source /etc/profile

添加到自動(dòng)啟動(dòng)

vim /etc/init.d/boot.local
/data/dbdata_3306/mysqld start
/data/dbdata_3307/mysqld start
/data/dbdata_3308/mysqld start

如果是rhel或者centos系統(tǒng)的話自啟動(dòng)文件/etc/rc.local

管理的話,在本地都是采用 -S /data/dbdata_3308/mysql.sock,如果在遠(yuǎn)程可以通過(guò)不同的端口連接上去坐管理操作。其他的和單實(shí)例的管理沒什么區(qū)別!

再來(lái)看第二種通過(guò)官方自帶的mysqld_multi來(lái)實(shí)現(xiàn)多實(shí)例實(shí)戰(zhàn):

這里的mysql安裝以及數(shù)據(jù)庫(kù)的初始化和前面的步驟一樣,就不再贅述。

mysqld_multi的配置

vim /etc/my.cnf

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = admin
password = password

[mysqld1]
socket = /data/dbdata_3306/mysql.sock
port = 3306
pid-file = /data/dbdata_3306/3306.pid
datadir = /data/dbdata_3306
user = mysql
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120

[mysqld2]
socket = /data/dbdata_3307/mysql.sock
port = 3307
pid-file = /data/dbdata_3307/3307.pid
datadir = /data/dbdata_3307
user = mysql
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120

[mysqld3]
socket = /data/dbdata_3308/mysql.sock
port = 3308
pid-file = /data/dbdata_3308/3308.pid
datadir = /data/dbdata_3308
user = mysql
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
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 = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120

[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\u@\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192

mysqld_multi啟動(dòng)

/usr/local/mysql/bin/mysqld_multi start 1
/usr/local/mysql/bin/mysqld_multi start 2
/usr/local/mysql/bin/mysqld_multi start 3

或者采用一條命令的形式:

/usr/local/mysql/bin/mysqld_multi start 1-3

更改原來(lái)密碼(處于安全考慮,還需要?jiǎng)h除系統(tǒng)中沒有密碼的帳號(hào),這里省略了):

/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3306/mysql.sock
/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3307/mysql.sock
/usr/local/mysql/bin/mysqladmin -uroot password 'password' -S /data/dbdata_3308/mysql.sock

登錄測(cè)試并創(chuàng)建admin密碼(停止mysql的時(shí)候需要使用到)

/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;
/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;
/usr/local/mysql/bin/mysql -uroot -ppassword -S /data/dbdata_3308/mysql.sock
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password';
flush privileges;

更改環(huán)境變量

vim /etc/profile
PATH=${PATH}:/usr/local/mysql/bin/
source /etc/profile

添加到自動(dòng)啟動(dòng)

vim /etc/init.d/boot.local
/usr/local/mysql/bin/mysqld_multi start 1-3

如果是rhel或者centos系統(tǒng)的話自啟動(dòng)文件/etc/rc.local

管理的話,在本地都是采用 -S /data/dbdata_3308/mysql.sock,如果在遠(yuǎn)程可以通過(guò)不同的端口連接上去坐管理操作。其他的和單實(shí)例的管理沒什么區(qū)別!

以上是“MySQL中多實(shí)例配置和管理的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI