MariaDB單機多實例部署過程記錄

後端存儲系統單機部署多實例的狀況並很少見,基本上都以專有服務器(Dedicated Server)的方式來部署。因此單機多實例基本上在測試環境會相對比較常見。另外,我的以爲生產服務器還有一種場景或許可能會用到單機多實例。好比對於前置MySQL代理服務的架構中,爲了未來遷移起來更加容易,並且單機單多實例相對於單機單實例,沒有太多性能降低的狀況下,仍是值得一試的。畢竟之後性能調整,只要增長服務器,遷移數據,不須要從新建庫建表,也沒有數據從新部署的問題。php

本文將記錄CentOS7上部署MariaDB多實例的過程。html

第一步、安裝系統、更新系統、安裝MariaDB最新版本(過程略)。node

第二步、調整系統參數 fs.aio-max-nrmysql

$ cat /etc/sysctl.confsql

# Increase size of file handles and inode cache
fs.file-max = 2097152
fs.aio-max-nr = 2097152

不增長這個參數可能會致使在打開MariaDB的開關選項innodb_use_native_aio時發生一個警告。具體位置/etc/sysctl.conf,修改完成後執行sysctl -p讓配置生效。後端

第三步、創建目錄,並初始化每個實例。centos

$ mkdir /opt/data{0,1} && chown mysql:mysql /opt/data{0,1}

$ mysql_install_db --datadir=/opt/data0 --user=mysql --force --defaults-file=/opt/init.ini

$ mysql_install_db --datadir=/opt/data1 --user=mysql --force --defaults-file=/opt/init.ini

# init.ini是一個不包含相關路徑的一個簡易配置,主要設定ibdata大小、ib_logfile數量和大小、undo文件的數量等等

第四步、修改配置文件my.cnf和my.cnf.d下每一個實例的配置bash

$ cat /etc/my.cnf服務器

[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

$ cat /etc/my.cnf.d/3306.cnf架構

[mysqld0]
basedir                               = /usr                                        #
character_set_server                  = utf8mb4                                     #
collation_server                      = utf8mb4_unicode_ci                          #
datadir                               = /opt/data0                                  #
default_storage_engine                = InnoDB                                      #
default_time_zone                     = '+8:00'                                     #
event_scheduler                       = 1                                           #
init_connect                          = 'set names utf8mb4;'                        #
lower_case_table_names                = 1                                           #
memlock                               = 0                                           #
pid_file                              = /opt/data0/mysql.pid                        #
port                                  = 3306                                        #
skip_external_locking                 = 1                                           #
socket                                = /opt/data0/mysql.sock                       #
tmpdir                                = /dev/shm                                    #
user                                  = mysql                                       #


general_log                           = 0                                           #
general_log_file                      = /opt/data0/general_query.log                #
log_output                            = FILE                                        #
log_error                             = /opt/data0/mysqld.log                       #
log_warnings                          = 2                                           #
log_queries_not_using_indexes         = 1                                           #
log_slow_admin_statements             = 1                                           #
log_slow_verbosity                    = query_plan                                  #
long_query_time                       = 1                                           #
min_examined_row_limit                = 100                                         #
slow_query_log                        = 1                                           #
slow_query_log_file                   = /opt/data0/slow_query.log                   #


gtid_strict_mode                      = 1                                           #
transaction_isolation                 = READ-COMMITTED                              #


innodb_data_home_dir                  = /opt/data0                                  #
innodb_data_file_path                 = ibdata1:16G:autoextend                      #
innodb_autoextend_increment           = 512                                         #
innodb_buffer_pool_size               = 30G                                         #
innodb_buffer_pool_instances          = 32                                          #
innodb_file_format                    = barracuda                                   #
innodb_file_format_max                = barracuda                                   #
innodb_compression_algorithm          = lzma                                        #
innodb_compression_level              = 6                                           #
innodb_buffer_pool_populate           = 1                                           #
innodb_log_group_home_dir             = /opt/data0                                  #
innodb_file_per_table                 = 1                                           #


innodb_undo_directory                 = /opt/data0                                  #
innodb_undo_logs                      = 128                                         #
innodb_undo_tablespaces               = 12                                          #


binlog_cache_size                     = 4M                                          #
binlog_checksum                       = crc32                                       #
binlog_format                         = row                                         #
expire_logs_days                      = 30                                          #
log_bin                               = /opt/data0/mysql-bin                        #
max_binlog_cache_size                 = 128M                                        #
max_binlog_size                       = 1G                                          #
sync_binlog                           = 1                                           #


max_relay_log_size                    = 1G                                          #
relay_log                             = /opt/data0/relay-bin                        #
relay_log_index                       = /opt/data0/relay-bin.index                  #
relay_log_info_file                   = relay-log.info                              #
relay_log_purge                       = 1                                           #
relay_log_recovery                    = 1                                           #
sync_relay_log                        = 10000                                       #


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links                        = 0                                           #

3307.cnf和3306.cnf不一樣在於:全部相關文件的路徑、監聽的端口號以及節點的名稱mysqld1。

第五步:systemd啓動文件

$ cat /etc/systemd/system/mariadb\@.service

#
# /etc/systemd/system/mariadb@.service
#

[Unit]
Description=Multi-Instance MariaDB Service
After=network.target
After=syslog.target


[Install]
WantedBy=multi-user.target


[Service]
User=mysql
Group=mysql
Type=forking
# true is needed for the ExecStartPre
PermissionsStartOnly=true
ExecStartPre=/usr/bin/mkdir -p /opt/data%i
ExecStartPre=/usr/bin/chown mysql:mysql /opt/data%i -R
ExecStart=/usr/bin/mysqld_multi start %i
ExecStop=/usr/bin/mysqld_multi stop %i
LimitNOFILE=102400
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=true

至此,MariaDB單機多實例應該就配置完成了,使用systemctl start mariadb@{0,1}同時啓動兩個實例,或者systemctl start mariadb@0單獨啓動一個實例。

如遇啓動問題,能夠根據提示查看相關提示,或者檢察/opt/data{0,1}/mysqld.log,看看系統日誌,並逐步排查。

當遇到存儲瓶頸,能夠將3307端口對應MariaDB服務和數據遷移到新服務器便可,過程不牽涉數據的從新部署,遷移起來相對更加容易。

 

 

 

參考文獻:

http://www.anntoin.com/Blog/Tutorials/MultipleDBInstances.html

https://forums.opensuse.org/showthread.php/504526-OS-13-1-MARIADB-Using-multiple-mysql-instance-without-a-supplementary-standalone-database

http://www.fromdual.ch/multi-instance-set-up-with-mysql-enterprise-server-5-7-on-rhel-7-with-systemd

http://unix.stackexchange.com/questions/256285/run-multiple-instances-of-mysql-centos7

相關文章
相關標籤/搜索