查看文章
 
在同个 Mysql 5 上运行多个实例
2008-03-17 6:59
在同个 Mysql 5 上运行多个实例
润名,Monday, August the fourteenth

一、前言:

  在Mysql中有一mysqld_multi命令,可用于在一台物理服务器运行多个Mysql服务,今天参考一些文档,亲自测试并通过,现将操作过程共享给大家!

操作系统: Redhat Enterprise AS 3,其它版的Linux应该也差不多.
数据库   : Mysql 5.0.22(RPM 安装)


规划:在一个 Mysql 上运行2个实例

比如运行端口分别是 3307 和 3308


二、在 Linux 下安装 Mysql 5

     先查看一下系统有没有安装了 Mysql,如果有的话,要先删除:

查看系统是否安装了 mysql
rpm -qa|grep mysql

卸载
rpm -e --nodeps mysqlxxxx

加上选项,--nodeps,是为了忽略错误


     Mysql 官方推荐使用 .rpm 文件安装 Mysql,去官方下载了Server和Client两个rpm 文件,进行安装:

rpm -ivh MySQL-server-standard-5.0.22-0.rhel3.i386.rpm
rpm -ivh MySQL-client-standard-5.0.22-0.rhel3.i386.rpm


三、mysqld_multi 介绍:

        根据Mysql管理手册中提到:每个Mysql的服务都可为独立的,所以它都调用一个my.cnf中各自不同的启动选项--就是下文中将提到的GNR值,使用不同的端口,生成各自的套接文件,服务的数据库都是独立的(更多可查阅mysql官方网站的英文管理手册).

        mysqld_multi是管理多个mysqld的服务进程,这些服务进程程序不同的unix socket或是监听于不同的端口。他可以启动、停止和监控当前的服务状态。

        程序在my.cnf(或是在--config-file自定义的配置文件)中搜索[mysqld#]段,"#"可以是任意的正整数。这个正整数就是在下面提及的段序列,即GNR。段的序号做为mysqld_multi的参数,来区别不同的段,这样你就可以控制特定mysqld进程的启动、停止或得到他的报告信息。这些组里的参数就像启动一个mysqld所需要的组的参数一样。但是,如果使用多服务,必须为每个服务指定一个unix socket或端口(摘自http://mifor.4dian.org中的使用mysqld_multi程序管理多个MySQL服务 )。


四、mysql_multi_exam.cnf 文件示例:

[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

#用一个帐号来启动所有的mysql服务器,因为是用一相同的帐号。那个么这帐号必须都是每个mysql服务都要用的帐号,最好是管理帐号,下面的口令与相同
#是mysql下的用户,非linux下的用户

#这个mysql要通过 root 给它赋权限,让它具有shutdown 和start mysql 的权限
#grant shutdown on *.* to 'boss'@'%' IDENTIFIED BY 'boss'

user = boss
password = boss

[mysqld1]
socket = /data/mysqldata/master/mysql.sock
port = 3307
pid-file = /data/mysqldata/master/mysql.pid
datadir = /data/mysqldata/master
user = boss

skip-locking
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id = 1

# Uncomment the following if you want to log updates
#log-bin=mysql-bin

# Uncomment the following if you are NOT using BDB tables
#skip-bdb

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
#innodb_log_arch_dir = /var/lib/mysql/
# 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

[mysqld2]
socket = /data/mysqldata/slave/mysql.sock
port = 3308
pid-file = /data/mysqldata/slave/mysql.pid
datadir = /data/mysqldata/slave
user = boss

skip-locking
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id = 1

# Uncomment the following if you want to log updates
#log-bin=mysql-bin

# Uncomment the following if you are NOT using BDB tables
#skip-bdb

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
#innodb_log_arch_dir = /var/lib/mysql/
# 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

[isamchk]
key_buffer = 8M
sort_buffer_size = 8M

[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

    
      从以上配置可以看出,我的配置文件中有mysqld1,mysqld2两个实例。就是说我将启动2个mysql服务在同一服务器的不同端口--3307和3308,每datadir所指定的数据库文件路径都是不相同的


五、利用 Mysqld_multi 启动/停止 多实例 Mysql

说明:
使用如下参数来启动mysqld_multi: (注:该命令在mysql的bin目录中,根据上面所提到./configure --prefix=/usr/local/mysql ,所以该文件应该在 /usr/local/mysq/bin, 这得根据你安装时所指定的路径 )
db-app:/ # mysqld_multi [options] {start|stop|report} [GNR[,GNR]...]

start,stop和report是指你想到执行的操作。你可以在单独的服务或是多服务上指定一个操作,区别于选项后面的GNR列表。如果没有指定GNR列表,那么mysqld_multi将在所有的服务中根据选项文件进行操作。

每一个GNR的值是组的序列号或是一个组的序列号范围。此项的值必须是组名字最后的数字,比如说如果组名为mysqld17,那么此项的值则为17.如果指定一个范围,使用"-"(破折号)来连接二个数字。如GNR的值为10-13,则指组mysqld10到组mysqld13。多个组或是组范围可以在命令行中指定,使用","(逗号)隔开。不能有空白的字符(如空格或tab),在空白字符后面的参数将会被忽略。 (注:GNR值就是我们定义my.cnf中mysqld#中的值,我这里只有1-4).

启动 mysql
mysqld_multi --config-file=/usr/share/mysql/mysql_multi_exam.cnf start 1-2

停止 mysql
mysqld_multi --config-file=/usr/share/mysql/mysql_multi_exam.cnf stop 1-2

   


六、客户端访问

说明:
任何客户端访问都需要指定访问端口。方才能进入指定数据库服务.否则将使用到Mysql默认的端口(3306)所服务的MYSQL,如:

mysql -uboss -pboss -P3307 -h127.0.0.1
mysql -uboss -pboss -P3308 -h127.0.0.1


七、常见问题:

     a、/usr/bin/mysqld_multi 的启动错误



1. WARNING! my_print_defaults command not found!
....
2. FATAL ERROR: Tried to use mysqladmin in group [mysqld1],
but no mysqladmin binary was found.Please add "mysqladmin=..."
in group [mysqld_multi], or in group [mysqld1].
....

解决办法,#export PATH=$PATH:/usr/bin



     b、启动 Mysql 客户端,老报 /var/lib/mysql/temp.sock 的错误


返回 /var/lib/mysql/temp.sock 的错误,一定要记住 加入-P 和-h才可以,如:

mysql -uboss -pboss -P3310 -h10.10.12.43
mysql -uboss -pboss -P3311 -h10.10.12.43

-h:带ip,如本机是 127.0.0.1
-P:mysql 实例端口,如 3307

类别:数据库||添加到搜藏 |分享到i贴吧|浏览(5931)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu