1. 程式人生 > >Linux下MySQL5.6.36多例項

Linux下MySQL5.6.36多例項

安裝依賴環境

yum install ncurses-devel libaio-devel cmake -y

建立管理使用者

useradd -s /sbin/nologin -M mysql
id mysql

建立目錄

mkdir /application &&cd /application

下載MySQL軟體包

wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36.tar.gz

解壓

tar xf mysql-5.6.36.tar.gz
cd mysql-5.6.36

編譯安裝

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \
-DMYSQL_DATADIR=/application/mysql-5.6.36/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
make && make install

建立連線

ln -s /application/mysql-5.6.36/ /application/mysql

給予安裝檔案可執行許可權

chmod +x /application/mysql/scripts/mysql_install_db

建立多例項目錄

mkdir /data/330{6,7}/ -p

建立配置檔案

cat >> /data/3306/my.cnf <<EOF
[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_open_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
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 6
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/3306.err
pid-file=/data/3306/mysqld.pid
EOF

建立MySQL啟動檔案

vim /data/3306/mysql
#!/bin/sh
#init
port=3306
mysql_user="root"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path=/data/${port}/${port}.pid
start(){
    if [ ! -e "$mysql_sock" ];then
     	printf "Starting MySQL...\n"
        /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
        sleep 3
    else
        printf "MySQL is running...\n"
        exit 1
    fi
}
stop(){
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit 1
    else
        printf "Stoping MySQL...\n"
        mysqld_pid=`cat "$mysqld_pid_file_path"`
         if (kill -0 $mysqld_pid 2>/dev/null)
           then
             kill $mysqld_pid
             sleep 2
         fi
    fi
}

restart(){
    printf "Restarting MySQL...\n"
    stop
    sleep 2
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

給多例項建立配置檔案和啟動檔案

cp /data/3306/my.cnf mysql /data/3307/
sed -i  "s#3306#3307#g"

檢查

[[email protected] /]# tree /data
/data
├── 3306
│ ├── my.cnf
│ └── mysql
└── 3307
    ├── my.cnf
    └── mysql

建立資料庫目錄並授權

mkdir /data/{3306,3307}/data -p
chown -R mysql.mysql /data/
find /data -name mysql|xargs chmod 700
find /data -name mysql|xargs ls -l

初始化資料庫

  • 3306
cd /application/mysql/scripts
./mysql_install_db  --defaults-file=/data/3306/my.cnf --basedir=/application/mysql/ --datadir=/data/3306/data --user=mysql
  • 3307
./mysql_install_db  --defaults-file=/data/3307/my.cnf --basedir=/application/mysql/ --datadir=/data/3307/data --user=mysql

啟動多例項

/data/3306/mysql start
/data/3307/mysql start
netstat -lntup|grep 330

連線多例項

cp /application/mysql/bin/* /user/local/sbin/ 
mysql -S /data/3306/mysql.sock 
mysql -S /data/3307/mysql.sock
  • 3306
    在這裡插入圖片描述
  • 3307
    在這裡插入圖片描述