配置MySQL數據庫單機多實例

一、什麼是MySQL多實例

    簡單的說就是在一臺機器上開啓多個不同的服務端口(例如:3306、3307),運行多個mysql服務進程,這些服務進程通過不同的socket監聽不同的服務端口來提供各自的服務。

    這些mysql多實例共用一套mysql安裝程序,使用不同(也可以相同)的my.cnf配置文件、啓動程序、數據文件。在提供服務時,多實例mysql在邏輯上看來是各自獨立的,多個實例是根據配置文件中配置的參數來獲取服務器相關硬件資源。

二、MySQL常見應用場景

    由於公司業務訪問量不是很大,服務器的資源基本都是浪費的,這時候很適合使用多實例的應用,如果對SQL語句優化做的比較好,mysql多實例是一個很值得使用的技術,即使併發很大,合理分配系統資源,也不會有太大問題。

三、MySQL多實例常見配置方案

3.1 安裝MySQL數據庫

 具體安裝方法,請參見 編譯方式安裝MySQL數據庫 。

3.2 配置多實例

 3.2.1 創建多實例目錄

[root@mysql-multi ~]# mkdir -p /data/{3306,3307}/data
[root@mysql-multi ~]# tree /data        
/data  
├── 3306        
│   └── data        
└── 3307
     └── data

 3.2.2 將數據目錄及臨時目錄授權mysql用戶(在安裝mysql之前已經創建mysql用戶組和用戶)

[root@mysql-multi ~]# chown -R mysql.mysql /data
[root@mysql-multi ~]# chmod -R 1777 /tmp
[root@mysql-multi ~]# ls -ld /data/{3306,3307}/data        
drwxr-xr-x. 2 mysql mysql 4096 1月  18 22:40 /data/3306/data        
drwxr-xr-x. 2 mysql mysql 4096 1月  18 22:40 /data/3307/data

 3.2.3 創建配置文件my.cnf

3306端口
3307端口

[client]
port            = 3306
socket          = /data/3306/mysql.sock


[mysql]
no-auto-rehash


[mysqld]
user    = mysql
port    = 3306
socket  = /data/3306/mysql.sock
basedir = /application/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]
log-error=/data/3306/err_mysql_3306.err
pid-file=/data/3306/mysqld.pid

[client]
port            = 3307
socket          = /data/3307/mysql.sock

[mysql]
no-auto-rehash


[mysqld]
user    = mysql
port    = 3307
socket  = /data/3307/mysql.sock
basedir = /application/mysql
datadir = /data/3307/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/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
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 = 3

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]
log-error=/data/3307/err_mysql_3307.err
pid-file=/data/3307/mysqld.pid

3.3 初始化數據庫表

初始化3306端口數據庫
[root@mysql-multi ~]# cd /application/mysql/scripts/
[root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
WARNING: The host 'mysql-multi' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
......此處省略

初始化3307端口數據庫
[root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
WARNING: The host 'mysql-multi' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
......此處省略

  說明:一般情況下初始化數據庫表時日誌顯示兩個“OK”即表示初始化成功。

3.4 啓動數據庫多實例

  [root@mysql-multi ~]# /data/3306/mysql start
  Starting MySQL...
  [root@mysql-multi ~]# /data/3307/mysql start
  Starting MySQL...
  [root@mysql-multi ~]# netstat -lntup|grep 330
  tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28220/mysqld        
  tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      26267/mysqld

  說明:“mysql”是自己寫的啓動腳本就不貼出來了,mysql啓動和停止的實質爲:

   mysql數據庫啓動命令:

  # /application/mysql/bin/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

   mysql數據庫停止命令:

  # /application/mysql/bin/mysqladmin -u${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

3.5 配置環境變量

[root@mysql-multi ~]# echo "export PATH=/application/mysql/bin:$PATH" >> /etc/profile
[root@mysql-multi ~]# tail -1 /etc/profile
export PATH=/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@mysql-multi ~]# . /etc/profile

3.6 登錄數據庫

  登錄3306數據庫

[root@mysql-multi ~]# mysql -S /data/3306/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

  登錄3307數據庫

[root@mysql-multi ~]# mysql -S /data/3307/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

 說明:MySQL數據庫在初始登錄未設置登錄用戶名和密碼,可以在登錄數據庫之後增加賬戶信息至此配置MySQL數據庫多實例完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章