MySQL多執行個體學習筆記

來源:互聯網
上載者:User

標籤:mysql多執行個體 主從複製   資料庫基礎操作

MySQL多執行個體

簡單的說就是在一台機器上開啟多個不同的服務連接埠(如3306,3307等)運行多個MySQL服務進程,這些服務進程通過不同個socket監聽不同的服務連接埠來提供各自的服務


多執行個體的作用與問題

1、有效利用伺服器

2、節約伺服器資源

3、資源互相搶佔問題



多執行個體應用情境

1、資金緊張型公司

2、並發訪問不是特別大的業務

3、門戶網站應用程式MySQL多執行個體情境


1、建立MySQL多執行個體的資料檔案目錄

先關掉我們之前啟動的單一實例

[[email protected] ~]# /etc/init.d/mysqld stop

[[email protected] ~]# mv /etc/init.d/mysqld /etc/init.d/dan.mysqld


[[email protected] ~]#  mkdir -p /data/{3306,3307}/data

[[email protected] ~]# tree /data         

/data

|-- 3306

|   `-- data

`-- 3307

    `-- data


4 directories, 0 files


多執行個體的是在我們之前上面文章操作單一實例的基礎上進行的,只是在單一實例進行到

ln -s /application/mysql-5.5.32/ /application/mysql  這一步時候就不再往下面操作了,這個不清楚的話可以參考上篇文章安裝,這裡就不再重複操作了


2、建立MySQL多執行個體的設定檔

[[email protected] ~]# vim /data/3306/my.cnf[client]port            = 3306socket          = /data/3306/mysql.sock[mysql]no-auto-rehash[mysqld]user    = mysqlport    = 3306socket  = /data/3306/mysql.sockbasedir = /application/mysqldatadir = /data/3306/dataopen_files_limit    = 1024back_log = 600max_connections = 800max_connect_errors = 3000table_cache = 614external-locking = FALSEmax_allowed_packet =8Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 100thread_concurrency = 2query_cache_size = 2Mquery_cache_limit = 1Mquery_cache_min_res_unit = 2k#default_table_type = InnoDBthread_stack = 192K#transaction_isolation = READ-COMMITTEDtmp_table_size = 2Mmax_heap_table_size = 2Mlong_query_time = 1#log_long_format#log-error = /data/3306/error.log#log-slow-queries = /data/3306/slow.logpid-file = /data/3306/mysql.pidlog-bin = /data/3306/mysql-binrelay-log = /data/3306/relay-binrelay-log-info-file = /data/3306/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mexpire_logs_days = 7key_buffer_size = 16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors = 1032,1062replicate-ignore-db=mysqlserver-id = 1innodb_additional_mem_pool_size = 4Minnodb_buffer_pool_size = 32Minnodb_data_file_path = ibdata1:128M:autoextendinnodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 4Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0[mysqldump]quickmax_allowed_packet = 2M[mysqld_safe]log-error=/data/3306/mysql_martin3306.errpid-file=/data/3306/mysqld.pidvim /data/3307/my.cnf[client]port            = 3307socket          = /data/3307/mysql.sock[mysql]no-auto-rehash[mysqld]user    = mysqlport    = 3307socket  = /data/3307/mysql.sockbasedir = /application/mysqldatadir = /data/3307/dataopen_files_limit    = 1024back_log = 600max_connections = 800max_connect_errors = 3000table_cache = 614external-locking = FALSEmax_allowed_packet =8Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 100thread_concurrency = 2query_cache_size = 2Mquery_cache_limit = 1Mquery_cache_min_res_unit = 2k#default_table_type = InnoDBthread_stack = 192K#transaction_isolation = READ-COMMITTEDtmp_table_size = 2Mmax_heap_table_size = 2M#long_query_time = 1#log_long_format#log-error = /data/3307/error.log#log-slow-queries = /data/3307/slow.logpid-file = /data/3307/mysql.pid#log-bin = /data/3307/mysql-binrelay-log = /data/3307/relay-binrelay-log-info-file = /data/3307/relay-log.infobinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mexpire_logs_days = 7key_buffer_size = 16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors = 1032,1062replicate-ignore-db=mysqlserver-id = 3innodb_additional_mem_pool_size = 4Minnodb_buffer_pool_size = 32Minnodb_data_file_path = ibdata1:128M:autoextendinnodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 4Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0[mysqldump]quickmax_allowed_packet = 2M[mysqld_safe]log-error=/data/3307/mysql_martin3307.errpid-file=/data/3307/mysqld.pid


3、建立多執行個體的啟動指令碼

[[email protected] ~]# vim /data/3306/mysql#!/bin/sh#initport=3306mysql_user="root"mysql_pwd="123456"CmdPath="/application/mysql/bin"mysql_sock="/data/${port}/mysql.sock"#startup functionfunction_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 functionfunction_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 functionfunction_restart_mysql(){    printf "Restarting MySQL...\n"    function_stop_mysql    sleep 2    function_start_mysql}case $1 instart)    function_start_mysql;;stop)    function_stop_mysql;;restart)    function_restart_mysql;;*)    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"esac[[email protected] ~]# vim /data/3307/mysql#!/bin/sh#initport=3307mysql_user="root"mysql_pwd="123456"CmdPath="/application/mysql/bin"mysql_sock="/data/${port}/mysql.sock"#startup functionfunction_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 functionfunction_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 functionfunction_restart_mysql(){    printf "Restarting MySQL...\n"    function_stop_mysql    sleep 2    function_start_mysql}case $1 instart)    function_start_mysql;;stop)    function_stop_mysql;;restart)    function_restart_mysql;;*)    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"esac[[email protected] ~]# tree /data/data|-- 3306|   |-- data|   |-- my.cnf|   `-- mysql`-- 3307    |-- data    |-- my.cnf    `-- mysql4 directories, 4 files[[email protected] ~]# chown -R mysql.mysql /data[[email protected] ~]# find /data -type f -name "mysql"|xargs ls -l-rw-r--r--. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql-rw-r--r--. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql[[email protected] ~]# find /data -type f -name "mysql"|xargs chmod 700 [[email protected] ~]# find /data -type f -name "mysql"|xargs ls -l     -rwx------. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql-rwx------. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql


4、初始化資料庫

[[email protected] mysql-5.5.32]# cd /application/mysql/scripts/[[email protected] scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3306/data/ --user=mysql[[email protected] scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3307/data/ --user=mysql


5、啟動資料庫

[[email protected] scripts]# /data/3306/mysql startStarting MySQL...[[email protected] scripts]# /data/3307/mysql start Starting MySQL...[[email protected] scripts]# ss -lantup|grep 330tcp    LISTEN     0      128                    *:3306                  *:*      users:(("mysqld",20906,12))tcp    LISTEN     0      128                    *:3307                  *:*      users:(("mysqld",21624,11))


6、登入資料庫測試

[[email protected] scripts]# mysql -S /data/3306/mysql.sock Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.32-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> [[email protected] scripts]# mysql -S /data/3307/mysql.sock  Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.32 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/85/7C/wKioL1elViWQtOeDAAIK7kMXLVQ981.png" title="1.png" alt="wKioL1elViWQtOeDAAIK7kMXLVQ981.png" />


7、兩個資料庫都成功了,大家可以建立一個執行個體3308,這裡就不再操作了,簡單看下最後的效果

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/85/7C/wKioL1elVrWQefrqAAGRogAfy1g188.png" title="2.png" alt="wKioL1elVrWQefrqAAGRogAfy1g188.png" />

本文出自 “厚德載物” 部落格,謝絕轉載!

MySQL多執行個體學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.