MySQL单机多实例配置(转载http://skypegnu1.blog.51cto.com/8991766/1441188)

系统运维 waitig 403℃ 百度已收录 0评论

注:多谢原作者的技术分享,转发备忘
测试环境:
操作系统 CentOS 6.4 x86
MySQL 5.5.38

1、什么是MySQL多实例?
MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307,3308),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的、互不干涉的服务。各个实例之间是相互独立的,监听在不同的socket文件和端口上,每个实例的datadir, port, socket, pid都是不同的。

2、MySQL多实例的优缺点
优点:
有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。
节约服务器资金。当资金紧张,但是又需要提供多个数据库服务时,这时多实例就可以派上用场
方便后期架构扩展。当项目启动初期,服务器压力不是太大,可以在一个服务器上部署多个mysql实例,方便后续架构扩展。
缺点:
资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降。

3、应用场景
采用了数据伪分布式架构的原因,而项目启动初期又不一定有那多的用户量,为此先一组物理数据库服务器,但部署多个实例,方便后续迁移;
为规避mysql对SMP架构不支持的缺陷,使用多实例绑定处理器的办法,把不同的数据库分配到不同的实例上提供数据服务;
一台物理数据库服务器支撑多个数据库的数据服务,为提高mysql复制的从机的恢复效率,采用多实例部署;
已经为双主复制的mysql数据库服务器架构,想部分重要业务的数据多一份异地机房的热备份,而mysql复制暂不支持多主的复制模式,且不给用户提供服务,为有效控制成本,会考虑异地机房部署一台性能超好的物理服务器,甚至外加磁盘柜的方式,为此也会部署多实例;
传统游戏行业的MMO/MMORPG,以及Web Game,每一个服都对应一个数据库,而可能要做很多数据查询和数据订正的工作,为减少维护而出错的概率,也可能采用多实例部署的方式,按区的概念分配数据库;

4、测试环境约定
1、将所有的安装文件、配置文件、数据目录全部放存/mydata/data目录中,便于今后实现快速迁移、整体备份和快速复制;
2、在一台服务器上配置2个MySQL实例,分别绑定在3306、3307端口。
3、实例均采用my-medium.cnf 配置文件;我们可以根据实际需求定制各个实例的my.cnf配置。

多实例可以有两种方案:
基于mysqld_multi: 多个实例共用同一个my.cnf配置文件中,利用[mysqld1]、[mysqld2]、[mysqld*]标签实现不同实例的差异化配置;
基于多配置文件:每一个实例单独一个my.cnf配置文件

第一种方案:每一个实例单独一个my.cnf配置文件

datadir:    /mydata/data/3306
            /mydata/data/3307
my.cnf:     /mydata/data/3306/my.cnf
            /mydata/data/3307/my.cnf

5、安装MySQL(通用二进制方式)
1、创建mysql用户和组
1
2

[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql

2、目录规划
我们为每个实例单独创建一个目录:3306, 3307

[root@localhost ~]# mkdir -pv /mydata/data/330{6,7}
mkdir: created directory `/mydata/data/3306'
mkdir: created directory `/mydata/data/3307'

[root@localhost ~]# tree /mydata/data/
/mydata/data/
|– 3306
`– 3307
3、解压

[root@localhost ~]# tar xf mysql-5.5.38-linux2.6-i686.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local/   
[root@localhost local]# ln -sv src/mysql-5.5.38-linux2.6-i686/ mysql
create symbolic link `mysql' to `src/mysql-5.5.38-linux2.6-i686/'

4、提供配置文件,并编辑

[root@localhost local]# cd mysql
[root@localhost mysql]# cp support-files/my-medium.cnf /mydata/data/3306/my.cnf
[root@localhost mysql]# cp support-files/my-medium.cnf /mydata/data/3307/my.cnf

# 这里是实验环境,所以简单配置。请各位看官根据实际需求调整

###3306
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql_3306.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql_3306.sock
pid-file        = /mydata/data/3306/mysql.pid
user            = mysql
basedir         = /usr/local/mysql
datadir         = /mydata/data/3306

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

###3307
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3307
socket          = /tmp/mysql_3307.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3307
socket          = /tmp/mysql_3307.sock
pid-file        = /mydata/data/3307/mysql.pid
user            = mysql
basedir         = /usr/local/mysql
datadir         = /mydata/data/3307

5、修改数据目录的属主、属组

[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3306
[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3307

6、把mysql/bin目录添加到PATH

[root@localhost mysql]# vi /etc/profile.d/mysql.sh
# 添加
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# . /etc/profile.d/mysql.sh

7、初始化

初始化 实例1

[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3306 --user=mysql

初始化 实例2

[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3307 --user=mysql

8、启动/关闭 实例
这里有一个问题,每个实例如何读取各自的my.cnf配置文件呢? 我们需要手动指定
/usr/local/mysql/bin/mysqld_safe
–defaults-file 手动指定配置文件

启动实例

[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe  --defaults-file=/mydata/data/3306/my.cnf &>/dev/null &
[1] 1526
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe  --defaults-file=/mydata/data/3307/my.cnf &>/dev/null &
[2] 1832

[root@localhost mysql]# netstat -tulpn | grep -i mysql
tcp        0      0 0.0.0.0:3306           0.0.0.0:*          LISTEN      1815/mysqld         
tcp        0      0 0.0.0.0:3307           0.0.0.0:*          LISTEN      2121/mysqld

关闭实例

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p -S /tmp/mysql_3306.sock shutdown
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p -S /tmp/mysql_3307.sock shutdown

注意:这里mysql的root用户并没有设置密码,密码提示直接敲 Enter即可

OK, 我们看到mysqld 监听在3306,3307两个端口上。
这里,我们也可以提供一个服务启动脚本来进行管理,大家去看看support-files/mysql.server这个服务启动脚本吧。
下面提供一个脚本模板:(大家根据需求更改)

#!/bin/bash

. /etc/init.d/functions

PORT=$2

USER=root
PASSWD=

MYSQLBIN='/usr/local/mysql/bin'
SOCKETFILE="/tmp/mysql_${PORT}.sock"
PIDFILE="/mydata/data/${PORT}/mysql.pid"
MYCNF="/mydata/data/${PORT}/my.cnf"

[[ $# -eq 2 ]] || {
    echo "Usage: $0 {start|stop|restart|reload}  {PORT}"
    exit 1
}


mysql_start() {
    [[ -e "$SOCKETFILE" ]] && {
        action "MySQL port: $PORT IS already running" /bin/false
        exit 0
    } || {
        action "Starting MySQL...  please wait" /bin/true
        $MYSQLBIN/mysqld_safe --defaults-file=$MYCNF &> /dev/null &
    }

    [[ "$?" == "0" ]] && {
        action "MySQL has been Started" /bin/true
    } || {
        action "MySQL Started" /bin/false
    }
}


mysql_stop() {
    [[ ! -e "$SOCKETFILE" ]] && {
        action "MySQL port:$PORT was already down" /bin/false
    } || {
        $MYSQLBIN/mysqladmin -u $USER -p$PASSWD -S $SOCKETFILE shutdown &>/dev/null
    }
    [[ "$?" == 0 ]] && {
        action  "MySQL port:$PORT has been Stopped" /bin/true
    } 
}

case "$1" in
'start')
    mysql_start
    ;;
'stop')
    mysql_stop
    ;;
'restart'|'reload')
    mysql_stop
    sleep 3
    mysql_start
    ;;
*)
    echo "Usage: $0 {start|stop|restart|reload}  {PORT}"
esac

9、利用脚本启动多实例

[root@localhost ~]# /etc/init.d/mysqld start 3306
Starting MySQL...  please wait                             [  OK  ]
MySQL has been Started                                     [  OK  ]
[root@localhost ~]# /etc/init.d/mysqld start 3307
Starting MySQL...  please wait                             [  OK  ]
MySQL has been Started                                     [  OK  ]
[root@localhost ~]# netstat -tulpn | grep 330
tcp        0      0 0.0.0.0:3306       0.0.0.0:*              LISTEN      2663/mysqld         
tcp        0      0 0.0.0.0:3307       0.0.0.0:*              LISTEN      2772/mysqld

10、配置多实例开启自启动
通过将mysql多实例的脚本与对应的参数放入/etc/rc.local文件,来实现mysql多实例的开机自启动

[root@localhost ~]# echo '/etc/init.d/mysqld start 3306' >> /etc/rc.local
[root@localhost ~]# echo '/etc/init.d/mysqld start 3307' >> /etc/rc.local
[root@localhost ~]# tail /etc/rc.local
tail: inotify cannot be used, reverting to polling
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/mysqld start 3306
/etc/init.d/mysqld start 3307

11、测试连接登录实例

我们需要指定通过哪一个套接字进行连接

注意:这里mysql的root用户并没有设置密码,所以可以直接登录

[root@localhost mysql]# /usr/local/mysql/bin/mysql -S /tmp/mysql_3306.sock 
[root@localhost mysql]# /usr/local/mysql/bin/mysql -S /tmp/mysql_3307.sock

12、安全配置
实例刚刚部署完之后的安全优化,主要有两方面:
1、为root设置密码
2、我们要查看mysql默认的用户,并将多余账户删除掉
这里仅仅以,端口为3306的mysql实例安全优化为例,做演示:

为mysql的root用户设定密码

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot password '123456' -S /tmp/mysql_3306.sock

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql_3306.sock
mysql> SHOW DATABASES;

删除test数据库

mysql> DROP DATABASE test;
Query OK, 0 rows affected (0.04 sec)

查看并删除多余用户

mysql> SELECT user, host FROM mysql.user;
+——+———————–+
| user | host |
+——+———————–+
| root | 127.0.0.1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+——+———————–+
5 rows in set (0.01 sec)

mysql> DELETE FROM mysql.user WHERE user=” AND host=’localhost’;
Query OK, 1 row affected (0.00 sec)

mysql> DELETE FROM mysql.user WHERE user=” AND host=’localhost.localdomain’;
Query OK, 1 row affected (0.01 sec)

mysql> DELETE FROM mysql.user WHERE user=’root’ AND host=’localhost.localdomain’;
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)
13、增加实例
1、建立新实例对应的目录并提供my.cnf配置文件
1
2
3
4
[root@localhost ~]# mkdir -pv /mydata/data/3308/
mkdir: created directory `/mydata/data/3308/’
[root@localhost ~]# cp /mydata/data/3306/my.cnf /mydata/data/3308/my.cnf
[root@localhost ~]# sed -i ‘s/3306/3308/g’ /mydata/data/3308/my.cnf
2、初始化端口为3308的mysql实例
1
[root@localhost mysql]# scripts/mysql_install_db /usr/local/mysql –datadir=/mydata/data/3308 –user=mysql
3、启动测试
1
2
3
4
5
6
7
[root@localhost ~]# /etc/init.d/mysqld start 3308
Starting MySQL… please wait [ OK ]
MySQL has been Started [ OK ]
[root@localhost ~]# netstat -tulpn | grep 330
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2663/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 2772/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 2932/mysqld

第二种方案:多个实例共用同一个my.cnf配置文件
通过mysql自带的 mysqld_multi 工具来实现多实例的部署和管理。
环境:
在一台服务器上通过源码或者二进制包安装MySQL 5.5以上版本。
数据目录均存放在 /mydata/data/目录,方便以后实现快速迁移、整体备份和复制。
在同一个服务器运行两个实例,一个绑定在3306端口,一个绑定在3307端口。
实践方案:
通过mysqld_multi方式来管理两个不同的实例,采用相同的配置文件共享性能优化配置参数
在同一个配置文件中,利用[mysqld3306]和[mysqld3307]标签实现不同实例的差异化配置。

datadir: /mydata/data/3306
/mydata/data/3307
my.cnf: /etc/my.cnf

一点基础知识:mysqld_multi
要配置MySQL多实例,首先我们需要了解一下mysqld_multi这个脚本。mysqld_multi是管理多个mysqld的服务进程。这些服务进程用不同的unix socket或是监听于不同的端口,通过简单的命令,它可以启动,关闭和报告所管理的服务器的状态 。
我们看一看官方的说明:
mysqld_multi is designedto manage several mysqld processes that listen for connections on differentUnix socket files and TCP/IP ports. It can start or stop servers, or reporttheir current status.
–如果我们的一个节点上有多个mysql,可以使用mysqld_multi 来管理。
mysqld_multi searchesfor groups named [mysqldN] in my.cnf (or in the file named by the –config-fileoption). N can be any positive integer. This number is referred to in thefollowing discussion as the option group number, or GNR. Group numbersdistinguish option groups from one another and are used as arguments to mysqld_multito specify which servers you want to start, stop, or obtain a status reportfor. Options listed in these groups are the same that you would use in the [mysqld]group used for starting mysqld. (See, for example, Section 2.10.1.2, “Startingand Stopping MySQL Automatically”.) However, when using multiple servers, it isnecessary that each one use its own value for options such as the Unix socketfile and TCP/IP port number. For more informationon which options must beunique per server in a multiple-server environment, see Section 5.6, “RunningMultiple MySQL Instances on One Machine”.
– mysqld_multi 会在my.cnf 里搜索mysqldN的参数配置。
大致步骤:
wKioL1SfqTHTnEHbAAFsoBGuwho008.jpg
wKiom1SfqICid0dTAAG5xe3v7Gk028.jpg
wKioL1SfqTGRpQaNAAE-8Upo-Og816.jpg

配置步骤:
由于前面的步骤与上述方式相同,所以这里只是简述一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

添加mysql用户和组

[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql

目录规划

[root@localhost ~]# mkdir -pv /mydata/data/330{6,7}
mkdir: created directory /mydata'
mkdir: created directory
/mydata/data’
mkdir: created directory /mydata/data/3306'
mkdir: created directory
/mydata/data/3307’

[root@localhost ~]# chown -R mysql:mysql /mydata/data/3306
[root@localhost ~]# chown -R mysql:mysql /mydata/data/3307

解压

[root@localhost ~]# tar xf mysql-5.5.38-linux2.6-i686.tar.gz -C /usr/local/src

创建链接

[root@localhost local]# ln -sv src/mysql-5.5.38-linux2.6-i686 mysql
mysql' ->src/mysql-5.5.38-linux2.6-i686’

把bin添加到PATH

[root@localhost mysql]# vi /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# . /etc/profile.d/mysql.sh
提供配置文件:/etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# cp support-files/my-small.cnf /etc/my.cnf
[root@localhost mysql]# vi /etc/my.cnf

The following options will be passed to all MySQL clients

[client]

password = your_password

port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8

Here follows entries for some specific programs

This server may run 2+ separate instances

So we use mysqld_multi to manage their services

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /mydata/data/mysqld_multi.log
user = root ## Used for stopping the server via mysqladmin

如果该地方不是root用户,下面则需要在数据库为该用户添加shutdown权限,否则mysqld_multi将无法关闭实例。

password =

[mysqld3306]
port = 3306
socket = /tmp/mysqld_3306.sock
pid-file = /mydata/data/3306/mysqld.pid
datadir = /mydata/data/3306
basedir = /usr/local/mysql
lc-messages-dir = /usr/local/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

[mysqld3307]
port = 3307
socket = /tmp/mysqld_3307.sock
pid-file = /mydata/data/3307/mysqld.pid
datadir = /mydata/data/3307
basedir = /usr/local/mysql
lc-messages-dir = /usr/local/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

The MySQL server

[mysqld]
port = 3306
socket = /tmp/mysql.sock
pid-file = /mydata/data/mysqld.pid
datadir = /mydata/data
basedir = /usr/local/mysql
lc-messages-dir = /usr/local/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

Incrase the max connections

max_connections = 200

Set expiration time for logs, including binlogs

expire_logs_days = 14

set the character as utf8

character-set-server = utf8
collation-server = utf8_unicode_ci

skip-networking

server-id = 1

Uncomment the following if you want to log updates

log-bin=mysql-bin

binary logging format – mixed recommended

binlog_format=mixed

set engine

default-storage-engine = INNODB

enable per table data for innodb to shrink ibdata

innodb_file_per_table = 1

Uncomment the following if you are using InnoDB tables

innodb_data_home_dir = /usr/local/mysql/data

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/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

Remove the next comment character if you are not familiar with SQL

safe-updates

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

[mysql.server]
user = mysql

[mysqld_safe]
log-error = /mydata/data/mysqld.log
pid-file = /mydata/data/mysqld.pid
open-files-limit = 8192

初始化实例:
1
2
3
4
5

初始化实例:mysqld3306

[root@localhost mysql]# scripts/mysql_install_db –basedir=/usr/local/mysql –datadir=/mydata/data/3306 –user=mysql

初始化实例:mysqld3307

[root@localhost mysql]# scripts/mysql_install_db –basedir=/usr/local/mysql –datadir=/mydata/data/3307 –user=mysql
提供多实例管理脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@localhost mysql]# cp support-files/mysqld_multi.server /etc/init.d/
[root@localhost mysql]# vi /etc/init.d/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=/usr/local/mysql
bindir=/usr/local/mysql/bin

conf = /etc/my.cnf
export PATH=PATH:bindir

if test -x bindir/mysqldmultithenmysqldmulti=bindir/mysqld_multi”;
else
echo “Can’t execute bindir/mysqldmultifromdirbasedir”;
exit;
fi

case “1instart)mysqld_multi” –defaults-extra-file=confstart2
;;
‘stop’ )
mysqldmultidefaultsextrafile=conf stop 2;;report)mysqld_multi” –defaults-extra-file=confreport2
;;
‘restart’ )
mysqldmultidefaultsextrafile=conf stop 2mysqld_multi” –defaults-extra-file=confstart2
;;
*)
echo “Usage: $0 {start|stop|report|restart}” >&2
;;
esac
管理MySQL实例:
– mysqld_multi 会在my.cnf 里搜索mysqldN的参数配置。
同时启动mysqld3306, mysqld3307
1
2
3
4
5
6
7
[root@localhost mysql]# /etc/init.d/mysqld_multi.server start 3306,3307

这里的3306, 3307 就是在 my.cnf 配置文件中定义的。[mysqld3306], [mysqld3307]

我们看到两个实例已经启动成功

[root@localhost mysql]# netstat -tulpn | grep -i mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2876/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 2877/mysqld
同时关闭mysqld3306, mysqld3307
1
2

先不要操作哈, 等测试登录后再关闭

[root@localhost mysql]# /etc/init.d/mysqld_multi.server stop 3306,3307
登录实例:
1
2
3
4
5
6
7
8
9

注意,因为这里mysql的root用户并没有设置密码,所以可以直接登录

登录mysqld3306

[root@localhost mysql]# mysql -uroot -S /tmp/mysqld_3306.sock

登录mysqld3307

[root@localhost mysql]# mysql -uroot -S /tmp/mysqld_3307.sock

或者

[root@localhost mysql]# mysql -uroot -h127.0.0.1 -P3306

总结:
这里实验仅配置2个实例,只要你的机器足够强劲,那么可以配置更多的实例。
本实验仅做了初步的设置,mysql的root用户密码, 以及其他匿名用户需要手动去设置或删除
非常困扰的是如何手动指定配置文件,以及Mysql读取配置文件的顺序
MySQL自带了几个不同的配置文件,放置在/opt/mysql/support-files目录下,分别是my-huge.cnf,my-innodb-heavy-4G.cnf,my-large.cnf,my-medium.cnf,my-small.cnf,通过名称我们可以很直观的了解到他们是针对不同的服务器配置的,本文中仅有的一点关于InnoDB的配置,是取自于my-small.cnf的,因为我是在虚拟机上进行的设置;在生产环境中,我们可以通过参考my-huge.cnf或my-innodb-heavy-4G.cnf中的部分参数配置,来对服务器进行优化;
关于MySQL缓存参数的优化,主要用于提升I/O能力,可以参考这里:http://heylinux.com/archives/1389.html
在单机运行多实例的情况下,切忌使用 mysql -hlocalhost 或 直接忽略-h参数 登陆服务器,这应该算是MySQL的一个bug,就是如果使用localhost或忽略-h参数,而不是指定127.0.0.1的话,即使选择的端口是3307,还是会登陆到3306中去,因此应尽量避免这种混乱的产生,统一用127.0.0.1绑定端口 或 采用socket 来登陆;

问题:第二种方案 my.cnf 配置文件中,为什么还要配置 [mysqld] 呢??
还希望各位帮忙解答一下。

参考:
http://heylinux.com/archives/1689.html
http://blog.fity.cn/post/349/

本文出自 “Share your knowledge” 博客,请务必保留此出处http://skypegnu1.blog.51cto.com/8991766/1441188


本文由【waitig】发表在等英博客
本文固定链接:MySQL单机多实例配置(转载http://skypegnu1.blog.51cto.com/8991766/1441188)
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)