MySQL 多实例

Linux系统环境

[root@MySQL01 ~]#cat /etc/redhat-release                 #==》系统版本
CentOS release 6.7 (Final)
[root@MySQL01 ~]#uname –r                                #==》内核版本
2.6.32-573.el6.x86_64
[root@MySQL01 ~]# uname -m                               #==》系统架构
x86_64
[root@MySQL01 ~]#echo $LANG                             #==》系统字符集
en_US.UTF-8
[root@MySQL01 ~]# mysql –V                              #==》MySQL版本
mysql Ver 14.14 Distrib 5.5.62, for Linux (x86_64) using readline 5.1

什么是MySQL多实例

简单的说,就是一台服务器运行多个MySQL服务进程,对应的MySQL进程使用不同的服务端口,MySQL服务进程通过不同的socket监听不同的服务端口来提供各自己的服务。

MySQL多实例共用一套MySQL安装程序,使用不同的(可以相同)my.cnf配置文件、启动程序、数据文件。在提供服务时,多实例MySQL在逻辑上看是各自独立的,多个实例的自身是根据 配置文件对应的设定值,来取得服务的相关硬件资源多少。

MySQL多实例优缺点

1、有效利用服务器资源
当单个服务器资源有空剩余时,可以充分利用剩余的资源创建更多的MySQL实例提供更多的服务。

2、节约服务器资源
当公司资金紧张,但是数据库又需要多个并且需各自尽量独立提供服务或者需要主从同步等,MySQL多实例就再好不过了。

3、资源相互争抢问题
当某个服务实例并发很高或者有慢查询时,整个实例会消耗更多的内存、CPU、磁盘、IO资源,导致服务器上的其它实例提供服务的质量下降,这就相当于大家在一个房子的不同卧室(MySQL实例),需要上厕所(硬件的CPU、内存、磁盘的IO资源)时,一个占用了厕所,其他人都要等待。

一、MySQL多实例采用多配置文件部置方案
标注:本教程MySQL采用的源码安装mysql, 在此不讲解如何安装MySQL,请自行查看相关文档,只需把MySQL安装完成即可配置MySQL多实例(不必初始化MySQL也不必启动MySQL,以免得冲突)

1、配置环境变量和本地域名解析

[root@MySQL01 ~]# ln -s /application/mysql-5.5.62/ /application/mysql
[root@MySQL01 ~]# ls -l /application/
total 4
lrwxrwxrwx 1 root root 26 Jul 24 13:59 mysql -> /application/mysql-5.5.62/
drwxr-xr-x 13 root root 4096 Jul 24 13:59 mysql-5.5.62
[root@MySQL01 ~]# echo 'export PATH=/application/mysql/bin:$PATH' >> /etc/profile
[root@MySQL01 ~]# source /etc/profile
[root@MySQL01 ~]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@MySQL01 ~]# mysql -s /sbin/nologin -M mysql
[root@MySQL01 ~]# id mysql
uid=501(mysql) gid=501(mysql) groups=501(mysql)
[root@MySQL01 scripts]# hostname
MySQL01
[root@MySQL01 scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 MySQL01

2、创建MySQL多实例数据文件目录并授权mysql用户及mysql用户组

标注:生产硬件配置 memory 32G/双CPU 8核/SAS磁盘(转速15K)6*600G,可开启2-3个实例

[root@MySQL01 ~]# mkdir -p /data/{3306,3307}/data
[root@MySQL01 ~]# tree /data/
/data/ **#==****》总的多实例根目录(名称可自定义)**
├── 3306 **#==****》3306实例目录**
│└── data **#==****》3306实例的数据文件目录**
└── 3307 **#==****》3307的实例目录**
 └── data **#==****》3307实例的数据文件目录**
4 directories, 0 files
[root@MySQL01 ~]# chown -R mysql.mysql /data/
[root@MySQL01 ~]# ls -l /data/
total 8
drwxr-xr-x 3 mysql mysql 4096 Jul 24 14:07 3306
drwxr-xr-x 3 mysql mysql 4096 Jul 24 14:07 3307

3、MySQL配置3306实例my.cnf配置文件

[root@MySQL01 ~]# 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

4、MySQL配置3307实例my.cnf配置文件

[root@MySQL01 ~]# 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

5、初始化MySQL多实例的数据库文件

#==》初始化3306实例**
[root@MySQL01 ~]# cd /application/mysql/scripts/
[root@MySQL01 scripts]#
./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
Installing MySQL system tables...
190724 14:34:38 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190724 14:34:38 [Note] /application/mysql/bin/mysqld (mysqld 5.5.62) starting as process 29425 ...
OK
Filling help tables...
190724 14:34:39 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190724 14:34:39 [Note] /application/mysql/bin/mysqld (mysqld 5.5.62) starting as process 29432 ...
OK
#==》初始化3307实例**
[root@MySQL01 scripts]#
./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
Installing MySQL system tables...
190724 14:37:57 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190724 14:37:57 [Note] /application/mysql/bin/mysqld (mysqld 5.5.62) starting as process 29489 ...
OK
Filling help tables...
190724 14:37:57 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
190724 14:37:57 [Note] /application/mysql/bin/mysqld (mysqld 5.5.62) starting as process 29496 ...
OK

6、启动MySQL实例

**#==****》启动3306实例**
[root@MySQL01 ~]# mysqld --defaults-file=/data/3306/my.cnf &>/dev/null &
[1] 30419
**#==****》启动3307实例**
[root@MySQL01 ~]# mysqld --defaults-file=/data/3307/my.cnf &>/dev/null &
[2] 30437
[root@MySQL01 ~]# netstat -tlunp | grep 330
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 30419/mysqld       
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 30437/mysqld  

7、修改MySQL管理员root并登录MySQL实例数据库

#==》修改3306实例登录密码并登录**
[root@MySQL01 ~]# mysqladmin -u root password '123456' -S /data/3306/mysql.sock
[root@MySQL01 ~]# mysql -S /data/3306/mysql.sock -uroot -p123456

#==》修改3306实例登录密码并登录**
[root@MySQL01 ~]# mysqladmin -u root password '123456' -S /data/3307/mysql.sock
[root@MySQL01 ~]# mysql -S /data/3307/mysql.sock -uroot -p123456

8、停止MySQL实例

#==》停止3306实例
[root@MySQL01 ~]# mysqladmin -S /data/3306/mysql.sock shutdown
[1]- Done mysqld --defaults-file=/data/3306/my.cnf &>/dev/null
[root@MySQL01 ~]# netstat -tlunp | grep 330
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 30437/mysqld     

#==》停止3307实例**
[root@MySQL01 ~]# mysqladmin -S /data/3307/mysql.sock shutdown
[2]+ Done mysqld --defaults-file=/data/3307/my.cnf &>/dev/null
[root@MySQL01 ~]# netstat -tlunp | grep 330

你可能感兴趣的:(MySQL 多实例)