mysql5.5.32多實例配置

操作系統:CentOS release 6.7 (Final)

內核版本:2.6.32-573.el6.x86_64

mysql版本號:mysql-5.5.32

1)安裝mysql所需的依賴包

# adduser mysql -s /sbin/nologin -M
# mkdir -p /data/{3306,3307}/data
# tree /data/
/data/
├── 3306
│?? └── data
└── 3307
    └── data
4 directories, 0 files
# yum -y install ncurse-devel libaio-devel

2)安裝編譯mysql需要的軟件

# tar xf cmake-2.8.8.tar.gz 
# cd cmake-2.8.8
# ./configure
# gmake
# which cmake
/usr/bin/cmake

3)采用編譯安裝mysql

# ls -l |grep mysql
-rw-r--r--  1 root    root 186722932 6月  27 05:03 mysql-5.5.32-linux2.6-x86_64.tar.gz
-rw-r--r--  1 root    root  24596474 6月  27 05:10 mysql-5.5.32.tar.gz
說明:mysql-5.5.32-linux2.6-x86_64.tar.gz為二進制安裝包、mysql-5.5.32.tar.gz為源碼安裝包
# tar xf mysql-5.5.32.tar.gz 
# cd mysql-5.5.32
# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/application/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
# make
# make install

4)創建mysql多實例的數據文件目錄

# mkdir -p /data/{3306,3307}/data
# tree /data
# ln -s /application/mysql-5.5.32/ /application/mysql
# ll /application/mysql
lrwxrwxrwx 1 root root 26 10月  8 22:50 /application/mysql -> /application/mysql-5.5.32/
# cd /data/3306
# ls -lrt
總用量 12
drwxr-xr-x 2 root root 4096 10月  8 21:13 data
-rw-r--r-- 1 root root 1899 10月 29 2013 my.cnf
-rw-r--r-- 1 root root 1307 7月  15 2013 mysql

5)編輯mysql實例配置文件

a、創建3306實例配置文件

# vim /data/3306/my.cnf 
[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/mysql_oldboy3306.err
pid-file=/data/3306/mysqld.pid

b、創建3306實例啟動腳本

# vim /data/3306/mysql 
#!/bin/sh
#init
port=3306
mysql_user="root"
mysql_pwd="redhat12345"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_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 function
function_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 function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}
case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

c、創建3307實例配置文件

# vim /data/3307/my.cnf 
[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/mysql_oldboy3307.err
pid-file=/data/3307/mysqld.pid

d、創建3307實例啟動腳本

#!/bin/sh
#init
port=3307
mysql_user="root"
mysql_pwd="redhat12345"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_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 function
function_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 function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}
case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

6)配置mysql多實例的文件權限

# chown -R mysql.mysql /data
# find /data -name mysql|xargs ls -l
-rw-r--r-- 1 mysql mysql 1312 10月  8 23:03 /data/3306/mysql
-rw-r--r-- 1 mysql mysql 1312 10月  8 23:03 /data/3307/mysql
# find /data -name mysql|xargs chmod 700
# find /data -name mysql|xargs ls -l
-rwx------ 1 mysql mysql 1312 10月  8 23:03 /data/3306/mysql
-rwx------ 1 mysql mysql 1312 10月  8 23:03 /data/3307/mysql

7)將mysql相關命令加入全局路徑

# ls /application/mysql/bin/mysql
/application/mysql/bin/mysql
# echo 'export PATH=/application/mysql/bin:$PATH'>>/etc/profile
# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
# source /etc/profile
# echo $PATH
/application/mysql/bin:/application/mysql/bin/:/application/nginx/sbin/:/application/mysql/bin/:/application/nginx/sbin/:/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

8)初始化實例的數據庫文件

# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
# echo $?
0

# 啟動多實例

# /data/3306/mysql start
Starting MySQL...
# /data/3307/mysql start
Starting MySQL...

# 查看數據庫是否啟動

# netstat -tunlp | grep 330*
tcp        0      0 0.0.0.0:3306                0.0.0.0:*               LISTEN      28533/mysqld        
tcp        0      0 0.0.0.0:3307                0.0.0.0:*               LISTEN      29250/mysqld

# 配置mysql多實例數據庫開機自啟動

# echo "# mysql multi instances startup">>/etc/rc.local
# echo "/data/3306/mysql start">>/etc/rc.local 
# echo "/data/3307/mysql start">>/etc/rc.local

# 登錄mysql進行測試:

# mysql -S /data/3306/mysql.sock 
# mysql -S /data/3307/mysql.sock

9) mysql安全設置

# mysqladmin -u root -S /data/3306/mysql.sock password 'redhat12345'
# mysqladmin -u root -S /data/3307/mysql.sock password 'redhat12345'

10) 登錄進入mysql

# mysql -uroot -S /data/3306/mysql.sock -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
# find /data -type f -name "mysql" -exec chown root.root {} \;
# find /data -type f -name "mysql" -exec chmod 700 {} \;
# find /data -type f -name "mysql" -exec ls -l {} \;
-rwx------ 1 root root 1312 10月  8 23:03 /data/3306/mysql
-rwx------ 1 root root 1312 10月  8 23:03 /data/3307/mysql

11) mysql的啟停測試

# /data/3306/mysql stop
Stoping MySQL...
# netstat -tunlp | grep 330*
tcp        0      0 0.0.0.0:3307                0.0.0.0:*              LISTEN      29250/mysqld        
# /data/3306/mysql start
Starting MySQL...      
# netstat -tunlp | grep 330*
tcp        0      0 0.0.0.0:3306                0.0.0.0:*              LISTEN      30168/mysqld        
tcp        0      0 0.0.0.0:3307                0.0.0.0:*              LISTEN      29250/mysqld

12) 在遠程主機進行測試

a、服務器端創建數據庫并添加授權賬號
mysql> create database wanlong;
Query OK, 1 row affected (0.00 sec)
mysql> create user wan identified by "redhat";
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on wanlong.* to 'wan'@'10.10.10.%' identified by 'redhat' with 
grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wanlong            |
+--------------------+
5 rows in set (0.00 sec)

b、客戶端進行訪問:

# mysql -uwan -predhat -h 10.10.10.129
-u:用戶名
-p:密碼
-h:遠程主機
mysql>  select user();
+------------------+
| user()           |
+------------------+
| wan@10.10.10.130 |
+------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| wanlong            |
+--------------------+
3 rows in set (0.01 sec)

到此,mysql的多實例已經完成

原創文章,作者:Net21-冰凍vs西瓜,如若轉載,請注明出處:http://www.www58058.com/50489

(0)
Net21-冰凍vs西瓜Net21-冰凍vs西瓜
上一篇 2016-12-05
下一篇 2016-12-05

相關推薦

  • redis/twemproxy 客戶端連接異常

    對于一個DBA,客戶端連接異常問題可以說是家常便飯的事情,處理多了都想吐。 root cause無疑發生在三個地方,先找自身的原因,依次排查下去: 1)服務器端db的負載,如果負載太高,創建socket太慢引起超時。另外服務器端socket的個數太多,也可以導致創建連接需要很長的時間或者創建連接不成功。 2)網絡是夠有抖動,包括lvs/twemproxy重啟…

    Linux干貨 2015-04-03
  • lvm邏輯卷管理

    lvm邏輯卷管理: 允許對卷進行方便操作的抽象層,包括重新設定文件系統的大小 允許在多個物理設備間重新組織文件系統,將設備指定為物理卷 用一個或者多個物理卷來創建一個卷組 物理卷是用固定大小的物理區域(Physical Extent,PE)來定義的 在物理卷上創建的邏輯卷是由物理區域(PE)組成 可以在邏輯卷上創建文件系統 創建: 創建物理卷: pvcrea…

    Linux干貨 2016-09-01
  • GlusterFS測試報告-01

    一、GlusterFS基礎環境的介紹 1、關于GlusterFS文件系統和架構的介紹 http://jingyan.baidu.com/article/046a7b3ef65250f9c27fa9d9.html 2、實驗的目的 a. 利用多臺性能較低并且老舊的服務器,實現企業的云盤功能 b. GlusterFS服務端和客戶端的部署和配置 c. 實現Glust…

    Linux干貨 2016-07-22
  • linux路由轉發(軟路由)實驗

        一。實驗網絡拓撲             (R1,R2由 Linux 虛擬機添加雙網卡做軟路由實現)              &n…

    Linux干貨 2016-09-05
  • 處理交換文件和分區

    處理交換文件和分區 交換分區是系統RAM的補充 ?  基本設置包括:  1 創建交換分區或者文件  2 使用mkswap寫入特殊簽名  3 在/etc/fstab文件中添加適當的條目  4 使用swapon -a 激活交換空間 掛載交換分區 ? 啟用:swapon swapon [OPTION]……

    Linux干貨 2016-09-01
  • 初學Linux之文本處理工具和正則表達

    1、各種文本工具來查看、分析、統計文本 ?
    2、grep ?
    3、正則表達式和擴展正則表達式 ?
    4、egrep

    2017-12-09
欧美性久久久久