1. 程式人生 > >CentOS7下yum安裝MariaDB配置多例項

CentOS7下yum安裝MariaDB配置多例項

測試mysql 主從同步+Mycat,但是沒有更多的伺服器,聽說一臺伺服器可以搭建多個mysql

yum安裝MariaDB:

yum install mariadb mariadb-server

安裝完成後要進行手動開啟MySQL服務並初始化:

service mariadb start
[[email protected] ~]# mysql_secure_installation

 安裝成功,測試結果:

[[email protected] multiMysql]#  mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

exit;退出mysql,開始安裝多例:

[[email protected] home]# mkdir /home/multiMysql
[[email protected] home]# mkdir /home/multiMysql/{etc,socket,bin,datadir,tmp}

在/home目錄下建立multiMysql資料夾,並在裡面建立etc,socket,bin,datadir,tmp 這五個資料夾備用。
現在我們在datadir中建立3個資料夾以放置三個例項的資料檔案:3307,3308,3309

[[email protected] multiMysql]# mkdir /home/multiMysql/datadir/{3307,3308,3309}

然後用mysql_install_db來生成即將使用的多個例項的資料檔案,首先需要對/home/multiMysql進行遞迴授權防止之後的操作出現許可權不夠的情況:(但是不建議直接修改整個資料夾的許可權為777,許可權太大,風險太高),可以在配置完成之後把檔案的的許可權修改小一點

chmod -R 777 /home/multiMysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --user=mysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --user=mysql

--user是指mysql例項將使用的在linux系統中的使用者,最好命名為mysql,yum安裝後一般都有這個使用者,如果沒有可以自主建立:

groupadd mysql
adduser -g mysql mysql
[[email protected] multiMysql]# ls /home/multiMysql/datadir/3308/
aria_log.00000001  aria_log_control  mysql  performance_schema  test

如果裡面有檔案代表生成成功。

接下來我們來做多例項的配置:

先建立一個公用配置檔案:

mkdir /home/multiMysql/etc/my.cnf.d/
vim /home/multiMysql/etc/my.cnf.d/my.cnf
[mysqld]
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=\\[email protected]\\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

然後分別建立三個例項的配置檔案:

cd /home/multilMysql/ect
[[email protected] etc]# vim 3307.cnf 
[client]
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock
[mysqld]
datadir=/home/multiMysql/datadir/3307
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock

tmpdir=/home/multiMysql/tmp/3307

myisam-recover-options = BACKUP 

!includedir /home/multiMysql/etc/my.cnf.d

注意一定要寫:(否則會報錯)

tmpdir=/home/multiMysql/tmp/3307

myisam-recover-options = BACKUP 

然後把3307.cnf複製份:

[[email protected] etc]# cp 3307.cnf 3308.cnf

然後分別編輯複製的兩份配置檔案,把埠和socket進行修改:

[[email protected] etc]# vim 3308.cnf 
[client]
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock
[mysqld]
datadir=/home/multiMysql/datadir/3308
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock

tmpdir=/home/multiMysql/tmp/3308

myisam-recover-options = BACKUP 

!includedir /home/multiMysql/etc/my.cnf.d

在配置檔案中,port是例項的埠,socket是例項執行時的sock檔案,datadir是之前我們生成的資料庫檔案位置。
 

然後我們來編輯三個啟動指令碼:

[[email protected] bin]# vim /home/multiMysql/bin/mysql3307
#!/bin/bash
mysql_port=3307
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.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
[[email protected] bin]# vim /home/multiMysql/bin/mysql3308
#!/bin/bash
mysql_port=3308
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.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

因為是yum安裝,所以mysqld_safe和mysqladmin可以不用加路徑直接執行,另外mysql_port是指這個bash簡要開啟的例項的埠,mysql_username和mysql_userpassword為我們即將在例項中配置的可關閉mysql程序的mysql使用者名稱和密碼。
現在給兩個bash檔案許可權來執行,並嘗試開啟兩個例項:

[[email protected] bin]# chmod 777 /home/multiMysql/bin/mysql3307
[[email protected] bin]# chmod 777 /home/multiMysql/bin/mysql3308

先關閉yum安裝的預設mysql例項程序:

service mysqld stop
[[email protected] bin]# /home/multiMysql/bin/mysql3307 start
[[email protected] bin]# /home/multiMysql/bin/mysql3308 start

檢視程序:

[[email protected] multiMysql]#  ps -ef | grep mysqld
root      8033     1  0 14:46 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3307.cnf
mysql     8113  8033  0 14:46 pts/1    00:00:02 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3307.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3307/server187.err --pid-file=server187.pid --socket=/home/multiMysql/socket/mysql3307.sock --port=3307
root      9774     1  0 14:59 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf
mysql     9833  9774  0 14:59 pts/1    00:00:01 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3308.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/server187.err --pid-file=server187.pid
root     10252     1  0 15:02 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf
mysql    10332 10252  0 15:02 pts/1    00:00:01 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3308.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3308/server187.err --pid-file=server187.pid --socket=/home/multiMysql/socket/mysql3308.sock --port=3308
root     11638 13061  0 15:24 pts/1    00:00:00 grep --color=auto mysqld
[[email protected] multiMysql]# 

可以看到多個例項已經啟動,我們來嘗試連線三個例項的sock:

啟動例項之後,進行連線:

[[email protected] multiMysql]# mysql -u root  -S /home/multiMysql/socket/mysql3308.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '2018';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)