#!/bin/bash
# 编译安装mysql
yum install cmake ncurses ncurses-devel gcc gcc-c++ cmake -y
mkdir -p /tools /application
cd /tools/
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.50.tar.gz
tar -xf mysql-5.5.50.tar.gz
cd mysql-5.5.50
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.50 \
-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
[ $? -eq 0 ] && ln -s /application/mysql-5.5.50 /application/mysql || {
    echo "Installation fail."
    exit 1
}
cd

# 添加系统用户
useradd -M -s /sbin/nologin mysql

####################################################################

# 设置监听的端口(多实例)
for port in {3307..3312}
do

    # 部署数据目录
    mkdir -p /data/mysql_multi_instances/${port}
    
    #初始化数据库文件
    /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/mysql_multi_instances/${port} --user=mysql &>/dev/null
    [ $? -eq 0 ] && echo "DB ${port} install success" || {
        echo "DB ${port} install failed"
        continue
    }
    
    #配置文件my.cnf
    cat >/data/mysql_multi_instances/${port}/my.cnf<<EOF
[client]
port            = ${port}
socket          = /data/mysql_multi_instances/${port}/mysql.sock
[mysqld]
port            = ${port}
datadir         = /data/mysql_multi_instances/${port}
socket          = /data/mysql_multi_instances/${port}/mysql.sock
pid-file        = /data/mysql_multi_instances/${port}/mysql.pid
log-error       = /data/mysql_multi_instances/${port}/mysql.err
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
log-bin=mysql-bin
server-id       = ${port}
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF

# 启动
/application/mysql/bin/mysqld_safe --defaults-file=/data/mysql_multi_instances/${port}/my.cnf &>/dev/null &
[ $? -eq 0 ] && echo "Instances ${port} install success" || echo "Instances ${port} install failed"

done


以下为安装后检查端口监听情况

[root@localhost ~]# netstat -tulnp|grep mysql

tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      45946/mysqld        

tcp        0      0 0.0.0.0:3308                0.0.0.0:*                   LISTEN      46385/mysqld        

tcp        0      0 0.0.0.0:3309                0.0.0.0:*                   LISTEN      46730/mysqld        

tcp        0      0 0.0.0.0:3310                0.0.0.0:*                   LISTEN      47046/mysqld        

tcp        0      0 0.0.0.0:3311                0.0.0.0:*                   LISTEN      47479/mysqld        

tcp        0      0 0.0.0.0:3312                0.0.0.0:*                   LISTEN      47787/mysqld


登录其中之一

[root@localhost ~]# mysql -uroot -h 127.0.0.1 -P3307
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.50-log Source distribution

Copyright (c) 2000, 2016, 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>