一、mysql多实例的原理
mysql多实例,简单的说,就是在一台服务器上开启多个不同的mysql服务端口(如3306,3307),运行多个mysql服务进程。这些服务进程通过不同的socket监听不同的服务端口,来提供各自的服务。
这些mysql实例共用一套mysql安装程序,使用不同的my.cnf配置文件、启动程序、数据文件。在提供服务时,mysql多实例在逻辑上看来是各自独立的,各个实例之间根据配置文件的设定值,来取得服务器的相关硬件资源。
二、mysql多实例的特点
2.1 有效的利用服务器资源
当单个服务器资源有剩余时,可以充分利用剩余的服务器资源来提供更多的服务。
2.2 节约服务器资源
当公司资金紧张,但是数据库需要各自提供独立服务,而且需要主从同步等技术时,使用多实例就最好了。
2.3 出现资源互相抢占问题
当某个实例服务并发很高或者有慢查询时,会消耗服务器更多的内存、CPU、磁盘IO等资源,这时就会导致服务器上的其它实例提供访问的质量下降,出现服务器资源互相抢占的现象。
三、mysql多实例应用场景
3.1 资金紧张型公司的选择
当公司业务访问量不太大,又舍不得花钱,但同时又希望不同业务的数据库服务各自独立,而且需要主从同步进行等技术提供备份或读写分离服务时,使用多实例是最好不过的。
3.2 并发访问不是特别大的业务
当公司业务访问量不太大,服务器资源基本闲置的比较多,这是就很适合多实例的应用。如果对SQL语句优化的好,多实例是一个很值得使用的技术。即使并发很大,只要合理分配好系统资源,也不会有太大问题。
四、mysql5.5多实例部署方法
MySQL多实例部署有2种方法:一种利用mysqld_multi配置、一种多实例分别定义不同的配置文件。先聊下利用mysqld_multi 配置(MySQL-5.5.36.tar.gz、cmake-2.8.8.tar.gz)
需求描述:一台服务运行四个msyql实例,分别绑定3306、3307、3308、3309端口上,四个实例都开启binlog日志,数据目录分别存放在/data/mysql/data、/data/mysql/data2、/data/mysql/data3、/data/mysql/data4,四个实例采用innodb作为默认存储引擎,字符编码采用UTF-8;四个实例均采用相同性能的配置参数;
注意事项:在编译安装时,将数据库配置文件my.cnf以及data目录均指向到、data/mysql目录中;
通过mysqld_multi的方式来管理四个不同的实例,采用相同的配置文件共享性能优化配置参数;在同一个配置文件中,利用[mysqld1]、[mysqld2]、[mysqld3]、[mysqld4]标签实现不同实例的配置。
五、环境准备
1、
#yum install -y ntp
#ntpdate -s time.windows.com
#hwclock -w
2、安装MySQL需要的依赖包
# yum install ncurses-devel libaio-devel gcc gcc-c++ -y
3、安装上传工具(上传MySQL、cmake源码包到/src)
#yum install lrzsz -y
4、编译安装cmake
#cd /src
#tar xf cmake-2.8.8.tar.gz
#cd cmake-2.8.8
#./configure #无需加任何参数
#gmake #gmake install
#cd ..
六、安装mysql 5.5.36
1、创建mysql用户与组
#useradd -r mysql #创建系统用户,不登录系统(等同useradd mysql -s /sbin/nologin -M)
#id mysql #查看用户ID为系统用户
2、创建安装目录与数据存放目录
#mkdir /data/mysql #mkdir /data/mysql/data
3、授权安装目录与数据目录
#chown -R mysql.mysql /data/mysql
#chown -R mysql.mysql /data/mysql/data
4、开始安装mysql
#cd /src #tar xf mysql-5.5.36.tar.gz #cd mysql-5.5.36
[root@MySQL mysql-5.5.36]# cmake . -DCMAKE_INSTALL_PREFIX=/data/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DMYSQL_UNIX_ADDR=/data/mysql/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
[root@MySQL mysql-5.5.36]# make && make install
好了,到这里我们mysql就安装完了,下面我们为mysql配置多实例
七、新建支持多实例的配置文件(这里配置四个实例)
1、删除默认的数据目录
# cd /data/mysql #rm -rf data
2、创建多实例配置需要的目录
mysql]#mkdir etc tmp run log binlogs data data2 data3 data4
mysql]#chown -R mysql.mysql etc tmp run log binlogs data data2 data3 data4
3、提供配置文件(msyql自带几个不同的配置文件,我们复制my-small.cnf修改,生产环境可选择my-huge.cnf或my-innodb-heavy-4G.cnf)
#cd /src #cd /mysql-5.5.36
#cp support-files/my-small.cnf /data/mysql/etc/my.cnf
etc]#vim my.cnf
# This server may run 4+ separate instances. So we use mysqld_multi to manage their services.
[client]
default-character-
set
= utf8
[mysqld_multi]
mysqld =
/data/mysql/bin/mysqld_safe
mysqladmin =
/data/mysql/bin/mysqladmin
log =
/data/mysql/log/mysqld_multi
.log
user = root
#password =
# This is the general purpose database.
# The locations are default.
# They are left in [mysqld] in case the server is started normally instead of by mysqld_multi.
[mysqld1]
socket =
/data/mysql/run/mysqld
.sock
port = 3306
pid-
file
=
/data/mysql/run/mysqld
.pid
datadir =
/data/mysql/data
lc-messages-
dir
=
/data/mysql/share/english
# These support master - master replication
#auto-increment-increment = 4
#auto-increment-offset = 1 # Since it is master 1
log-bin =
/data/mysql/binlogs/bin-log-mysqld1
log-bin-index =
/data/mysql/binlogs/bin-log-mysqld1
.index
#binlog-do-db = # Leave this blank if you want to control it on slave
max_binlog_size = 1024M
# This is exlusively for mysqld2
# It is on 3307 with data directory /data/mysqld/data2
[mysqld2]
socket =
/data/mysql/run/mysqld
.sock2
port = 3307
pid-
file
=
/data/mysql/run/mysqld
.pid2
datadir =
/data/mysql/data2
lc-messages-
dir
=
/data/mysql/share/english
# Disable DNS lookups
#skip-name-resolve
# These support master - slave replication
log-bin =
/data/mysql/binlogs/bin-log-mysqld2
log-bin-index =
/data/mysql/binlogs/bin-log-mysqld2
.index
#binlog-do-db = # Leave this blank if you want to control it on slave
max_binlog_size = 1024M
# Relay log settings
#relay-log = /data/mysql/log/relay-log-mysqld2
#relay-log-index = /data/mysql/log/relay-log-mysqld2.index
#relay-log-space-limit = 4G
# Slow query log settings
#log-slow-queries = /data/mysql/log/slow-log-mysqld2
#long_query_time = 2
#log-queries-not-using-indexes
# This is exlusively for mysqld3
# It is on 3308 with data directory /data/mysqld/data3
[mysqld3]
socket =
/data/mysql/run/mysqld
.sock3
port = 3308
pid-
file
=
/data/mysql/run/mysqld
.pid3
datadir =
/data/mysql/data3
lc-messages-
dir
=
/data/mysql/share/english
#Disable DNS lookups
#skip-name-resolve
# These support master - slave replication
log-bin =
/data/mysql/binlogs/bin-log-mysqld3
log-bin-index =
/data/mysql/binlogs/bin-log-mysqld3
.index
#binlog-do-db = # Leave this blank if you want to control it on slave
max_binlog_size = 1024M
# This is exlusively for mysqld4
# It is on 3309 with data directory /data/mysqld/data4
[mysqld4]
socket =
/data/mysql/run/mysqld
.sock4
port = 3309
pid-
file
=
/data/mysql/run/mysqld
.pid4
datadir =
/data/mysql/data4
lc-messages-
dir
=
/data/mysql/share/english
# Disable DNS lookups
#skip-name-resolve
# These support master - slave replication
log-bin =
/data/mysql/binlogs/bin-log-mysqld4
log-bin-index =
/data/mysql/binlogs/bin-log-mysqld4
.index
#binlog-do-db = # Leave this blank if you want to control it on slave
max_binlog_size = 1024M
# The rest of the my.cnf is shared
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
basedir =
/data/mysql
tmpdir =
/data/mysql/tmp
socket =
/data/mysql/run/mysqld
.sock
port = 3306
pid-
file
=
/data/mysql/run/mysqld
.pid
datadir =
/data/mysql/data
lc-messages-
dir
=
/data/mysql/share/english
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
# Increase the max connections
max_connections = 2
# The expiration time for logs, including binlogs
expire_logs_days = 14
# Set the character as utf8
character-
set
-server = utf8
collation-server = utf8_unicode_ci
# This is usually only needed when setting up chained replication
#log-slave-updates
# Enable this to make replication more resilient against server crashes and restarts
# but can cause higher I/O on the server
#sync_binlog = 1
# The server id, should be unique in same network
server-
id
= 1
# Set this to force MySQL to use a particular engine/table-type for new tables
# This setting can still be overridden by specifying the engine explicitly
# in the CREATE TABLE statement
default-storage-engine = INNODB
# Enable Per Table Data for InnoDB to shrink ibdata1
innodb_file_per_table = 1
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /data/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /data/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 % of RAM
# but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
[mysql.server]
user = mysql
[mysqld_safe]
log-error =
/data/mysql/log/mysqld
.log
pid-
file
=
/data/mysql/run/mysqld
.pid
open
-files-limit = 8192
4、修改my.cnf读写权限
#chown -R root.root /data/mysql/etc
#chmod 600 /data/mysql/etc/my.cnf 好了,到这里我们的配置文件就设置完成了,。
八、初始化多实例数据库
#cd /data/mysql/scripts
scripts]#./mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
scripts]#./mysql_install_db --basedir=/data/mysql --datadir=/data/mysql2/data --user=mysql
scripts]#./mysql_install_db --basedir=/data/mysql --datadir=/data/mysql3/data --user=mysql
scripts]#./mysql_install_db --basedir=/data/mysql --datadir=/data/mysql4/data --user=mysql
九、提供管理脚本 mysqld_multi.server
创建管理脚本目录
[root@node1 mysql]
# mkdir /data/mysql/init.d
[root@node1 mysql]
# cp support-files/mysqld_multi.server init.d/
2.简单修改一下脚本2.提供管理脚本
[root@node1 mysql]
# cd init.d/
[root@node1 init.d]
# vim mysqld_multi.server
#!/bin/sh
#
# A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen.
# This script assumes that my.cnf file exists either in /etc/my.cnf or
# /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the
# mysqld_multi documentation for detailed instructions.
#
# This script can be used as /etc/init.d/mysql.server
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.
#
# Version 1.0
#
basedir=
/data/mysql
bindir=
/data/mysql/bin
conf=
/data/mysql/etc/my
.cnf
export
PATH=$PATH:$bindir
if
test
-x $bindir
/mysqld_multi
then
mysqld_multi=
"$bindir/mysqld_multi"
;
else
echo
"Can't execute $bindir/mysqld_multi from dir $basedir"
;
exit
;
fi
case
"$1"
in
'start'
)
"$mysqld_multi"
--defaults-extra-
file
=$conf start $2
;;
'stop'
)
"$mysqld_multi"
--defaults-extra-
file
=$conf stop $2
;;
'report'
)
"$mysqld_multi"
--defaults-extra-
file
=$conf report $2
;;
'restart'
)
"$mysqld_multi"
--defaults-extra-
file
=$conf stop $2
"$mysqld_multi"
--defaults-extra-
file
=$conf start $2
;;
*)
echo
"Usage: $0 {start|stop|report|restart}"
>&2
;;
esac
好了,到这里我们所有的配置就全部完成了,最好直接复制粘贴,别自己修改,要不很烦,容易错;下面我们打包备份一下。
九、整体备份方便后续迁移
[root@node1 ~]
# cd /data/
[root@node1 data]
# tar czvf mysql-5.5.36-full.tar.gz /data/mysql/
[root@node1 data]
# ll -h
总用量 128M
drwx------. 2 root root 16K 8月 17 18:42 lost+found
drwxr-xr-x 22 mysql mysql 4.0K 1月 6 22:08 mysql
-rw-r--r-- 1 root root 128M 1月 7 00:25 mysql-5.5.350-full.
tar
.gz
注,备份完成后,直接将mysql-5.5.36-full.tar.gz拿到其他服务器上,解压后便可以直接启用。嘿嘿,方便吧
九、启动、关闭MySQL多实例
1.同时启动四个mysql实例
(1).方法一:[root@node1 ~]
# /data/mysql/init.d/mysqld_multi.server start 1,2,3,4
(2).方法二:/data/mysql/init.d/mysqld_multi.server start 3306,3307,3308,3309
插入:启动时报错
[root@localhost data]# /data/mysql/init.d/mysqld_multi.server start 3306
/data/mysql/init.d/mysqld_multi.server: /data/mysql/bin/mysqld_multi: /usr/bin/perl: bad interpreter: No such file or directory
貌似提示注释器错误,没有/usr/bin/perl文件或者档案,
解决办法(安装perl跟perl-devel即可):
执行 yum -y install perl perl-devel
后在初始化数据库即可。
再运行启动服务:
[root@localhost ~]# /data/mysql/init.d/mysqld_multi.server start 1,2,3,4
[root@localhost ~]# /data/mysql/init.d/mysqld_multi.server start 3306,3307,3308,3309
[root@localhost ~]#
没有报错,有戏噢!建议大家在准备环境过程中安装Perl、perl-devel .
2.查看一下启动的实例
[root@node1 ~]
# netstat -ntulp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 31416
/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 31414
/mysqld
tcp 0 0 0.0.0.0:3309 0.0.0.0:* LISTEN 31420
/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31413
/mysqld
3.同时关闭四个mysql实例
方法一:[root@node1 ~]
# /data/mysql/init.d/mysqld_multi.server stop 1,2,3,4
或方法二:[root@node1 ~]
# /data/mysql/init.d/mysqld_multi.server stop 3306,3307,3308,3309
4.单独启动或关闭mysql实例
(1).启动一个实例
[root@node1 ~]
# /data/mysql/init.d/mysqld_multi.server start 1
[root@node1 ~]
# netstat -ntulp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 32221
/mysqld
(2).关闭一个实例 [root@node1 ~]
# /data/mysql/init.d/mysqld_multi.server stop 1
注,启动或关闭两个或者三个实例方法的上面相同这里就不再演示
十、登录mysql多实例
登录【msyql1】
/data/mysql/bin/mysql -uroot -h127.0.0.1 -P3306 -p 输入空密码登录
mysql> show databases;
登录【msyql12】
/data/mysql/bin/mysql -uroot -h127.0.0.1 -P3307 -p 输入空密码登录
mysql> show databases;
其他实例登录方式相同,只改变不同的连接端口,即可!
注意:单机多实例环境,切忌不要使用 mysql -hlocalhost 或 直接忽略-h参数登录服务器,这应该算是MySQL的一个bug,就是如果使用localhost或忽略-h参数,而不是指定127.0.0.1的话,即使选择的端口是3307,还是会登陆到3306中去,因此应尽量避免这种混乱的产生,统一用127.0.0.1绑定端口或采用socket来登录,在mysql5.5中你不指定-h127.0.0.1选项,你是无法登录的。
十一、其他管理MYSQL多实例
1、修改mysql的root用户密码
[root@node1 ~]
# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3306 password '123456'
[root@node1 ~]
# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3307 password '123456'
[root@node1 ~]
# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3308 password '123456'
[root@node1 ~]
# /data/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3309 password '123456'
2、为保证安全,还应该删除匿名连的空密码账号
mysql>use mysql;
#选择系统数据库mysql
mysql>
select
Host,User,Password from user;
#查看所有用户
mysql>delete from user where password=
""
;
#删除无密码账户
mysql>flush privileges;
#刷新权限
mysql>
select
Host,User,Password from user;
#确认密码为空的用户是否已全部删除
mysql>
exit
;
好了,今天就整理到这里;多实例分别定义不同的配置文件下一篇继续……,下班回家!