mysql5.5-编译安装(部署单实例与多实例)

  • 一、mysql单实例安装
    • 1.系统环境
    • 2.安装包
    • 3.安装所需的包
    • 4.安装cmake
    • 5.安装mysql
    • 6.初始化环境配置
      • 6.1配置文件模板
      • 6.2环境变量
      • 6.3目录授权
      • 6.4初始化数据库
      • 6.5启动脚本
      • 6.6启动mysql
      • 6.7设置Linux系统字符集
      • 6.8设置密码登录
    • 7.简单优化
      • 7.1清楚非管理员用户
      • 7.2删除test库
  • 二、mysql多实例安装
    • 1.停止mysql
    • 2.创建mysql多实例的目录
    • 3.创建3306与3307实例的配置文件
      • 3.1 3306实例配置
      • 3.2 3307实例配置
    • 4.配置多实例根目录权限
    • 5.初始化数据库
    • 6.启动两个实例
    • 7.设置密码
    • 8.关闭实例
    • 9.编写脚本管理实例启动与关闭
      • 9.1 3306实例管理脚本
      • 9.2 3307实例管理脚本
    • 10.授权脚本并测试

一、mysql单实例安装

1.系统环境

[root@mysql ~]# cat /etc/centos-release 
CentOS release 6.5 (Final)
[root@mysql ~]# uname -r
2.6.32-431.el6.x86_64

2.安装包

[root@mysql ~]# ll *.gz
-rw-r--r--. 1 root root  5691656 Sep 14 23:01 cmake-2.8.8.tar.gz
-rw-r--r--. 1 root root 24596474 Sep 14 22:48 mysql-5.5.32.tar.gz

3.安装所需的包

[root@mysql ~]# yum install gcc-c++  ncurses-devel libaio-devel cmake -y

4.安装cmake

[root@mysql ~]# tar xf cmake-2.8.8.tar.gz
[root@mysql ~]# cd cmake-2.8.8
[root@mysql cmake-2.8.8]# ./configure 
[root@mysql cmake-2.8.8]# gmake && gmake install

5.安装mysql

[root@mysql ~]# tar xf mysql-5.5.32.tar.gz 
[root@mysql ~]# cd mysql-5.5.32
[root@mysql mysql-5.5.32]# useradd mysql -s /sbin/nologin -M 
[root@mysql mysql-5.5.32]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 -DMYSQL_DATADIR=/application/mysql-5.6.34/data -DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock -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
(-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \没有指定字符集)
[root@mysql mysql-5.5.32]# make && make install
[root@mysql mysql-5.5.32]# ln -s /application/mysql-5.6.34/ /application/mysql

6.初始化环境配置

6.1配置文件模板

[root@mysql mysql-5.5.32]#  cp support-files/my-small.cnf /etc/my.cnf 
cp: overwrite `/etc/my.cnf'? yes

6.2环境变量

[root@mysql mysql-5.5.32]# echo 'export PATH=/application/mysql/bin:$PATH' >> /etc/profile
[root@mysql scripts]# source /etc/profile

6.3目录授权

[root@mysql mysql-5.5.32]# chown -R mysql.mysql /application/mysql

6.4初始化数据库

[root@mysql mysql-5.5.32]# cd /application/mysql/scripts/
[root@mysql scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/application/mysql

6.5启动脚本

[root@mysql scripts]# cp /root/mysql-5.5.32/support-files/mysql.server /etc/init.d/mysqld
[root@mysql scripts]# chmod +x /etc/init.d/mysqld 

6.6启动mysql

[root@mysql scripts]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 
[root@mysql scripts]# chkconfig mysqld on

6.7设置Linux系统字符集

[root@mysql ~]# echo "LANG="zh_CN.UTF-8"" > /etc/sysconfig/i18n 
[root@mysql ~]# cat /etc/sysconfig/i18n 
LANG=zh_CN.UTF-8
[root@mysql ~]# source /etc/sysconfig/i18n 

6.8设置密码登录

[root@mysql scripts]# mysqladmin -uroot password "000000"
[root@mysql scripts]# mysql -uroot -p000000
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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>

7.简单优化

7.1清楚非管理员用户

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
|      | mysql     |
| root | mysql     |
+------+-----------+
mysql> mysql> delete from mysql.user wher"";
Query OK, 2 rows affected (0.00 sec)
mysql> delete from mysql.user where host="::1";
Query OK, 1 row affected (0.00 sec)
mysql> delete from mysql.user where host="mysql";
Query OK, 1 row affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

7.2删除test库

mysql> drop database test;
Query OK, 0 rows affected (0.01 sec)

二、mysql多实例安装

1.停止mysql

[root@mysql ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS! 
[root@mysql ~]# ps -ef|grep mysqld
root      23903   1179  0 10:10 pts/0    00:00:00 grep mysqld

2.创建mysql多实例的目录

[root@mysql ~]# mkdir /data/{3306,3307}/data -p
[root@mysql ~]# tree /data/
/data/ #多实例的根目录
├── 3306 #3306实例的目录
│   └── data#3306实例的数据文件目录
└── 3307
    └── data

4 directories, 0 files

3.创建3306与3307实例的配置文件

3.1 3306实例配置

[root@mysql ~]# cat /data/3306/my.conf 
[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_qb3306.err  
pid-file=/data/3306/mysqld.pid

3.2 3307实例配置

[root@mysql ~]# cat /data/3307/my.conf
[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 = 2  

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_qb3307.err  
pid-file=/data/3307/mysqld.pid

4.配置多实例根目录权限

[root@mysql ~]# chown -R mysql:mysql /data/

5.初始化数据库

[root@mysql ~]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data/ --user=mysql
[root@mysql ~]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data/ --user=mysql

6.启动两个实例

[root@mysql ~]# mysqld_safe --defaults-file=/data/3306/my.conf >/dev/null 2>&1 &
[1] 24298
[root@mysql ~]# mysqld_safe --defaults-file=/data/3307/my.conf >/dev/null 2>&1 & 
[2] 25048
[root@mysql ~]# netstat -lntup|grep 330
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      25772/mysqld        
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      25022/mysqld

7.设置密码

[root@mysql ~]# mysqladmin -uroot password "000000" -S /data/3306/mysql.sock 
[root@mysql ~]# mysqladmin -uroot password "000000" -S /data/3307/mysql.soc
[root@mysql ~]# mysql -uroot -p000000 -S /data/3306/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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> quit
Bye
[root@mysql ~]# mysql -uroot -p000000 -S /data/3307/mysql.sock  
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> quit
Bye

8.关闭实例

[root@mysql ~]# mysqladmin -uroot -p000000 -S /data/3306/mysql.sock shutdown
[1]-  Done                    mysqld_safe --defaults-file=/data/3306/my.conf > /dev/null 2>&1
[root@mysql ~]# mysqladmin -uroot -p000000 -S /data/3307/mysql.sock shutdown 
[2]+  Done                    mysqld_safe --defaults-file=/data/3307/my.conf > /dev/null 2>&1
[root@mysql ~]# netstat -lntup|grep 330
[root@mysql ~]# netstat -lntup|grep 330|wc -l
0

9.编写脚本管理实例启动与关闭

9.1 3306实例管理脚本

[root@mysql ~]# cat /data/3306/mysqld 
#!/bin/bash
. /etc/init.d/functions 
Port=3306
user=root
password=000000
Path=/application/mysql/bin/
File=/data/${Port}/my.conf 
Sock=/data/${Port}/mysql.sock
mysql_start(){
        ${Path}mysqld_safe --defaults-file=${File} > /dev/null 2>&1 &
        sleep 3
        num=`netstat -lntup|grep ${Port}|wc -l`
        if [ "$num" -ne 0 ];then
                action "mysql $Port start......" /bin/true
        else
                action "mysql $Port start......" /bin/false
        fi
}
mysql_stop(){
        ${Path}mysqladmin -u${user} -p${password} -S $Sock shutdown
         num=`netstat -lntup|grep ${Port}|wc -l`
        if [ "$num" -eq 0 ];then
                action "mysql $Port stop......" /bin/true
        else    
                action "mysql $Port stop......" /bin/false
        fi
}
mysql_restart(){
        mysql_stop
        mysql_start
}
mysql_login(){
        ${Path}mysql -u${user} -p${password} -S $Sock
}
case $1 in
start)
        mysql_start
;;
stop)
        mysql_stop
;;
restart)
        mysql_restart
;;
login)
        mysql_login
;;
*)
        echo "Usage: $0 {start|stop|restart}"
esac

9.2 3307实例管理脚本

复制3306的脚本,修改端口即可
vim /data/3307/mysqld 
Port=3307

10.授权脚本并测试

[root@mysql ~]# chmod +x /data/{3306,3307}/mysqld
[root@mysql ~]# /data/3306/mysqld start
mysql 3306 start......                                     [确定]
[root@mysql ~]# /data/3307/mysqld start 
mysql 3307 start......                                     [确定]
[root@mysql ~]# netstat -lntup|grep 330
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      34332/mysqld        
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      33583/mysqld        

你可能感兴趣的:(Mysql)