Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26314969
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Mysql/postgreSQL

2010-02-24 13:31:31

转自网络:http://hi.baidu.com/baoobaoo/blog/item/3bd94f2a9432dc3c5343c199.html

一、前言:

  在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或端口(摘自中的使用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].
....

2.091224 20:03:05 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

091224 20:03:05 [ERROR] Aborting

091224 20:03:05 [Note] /usr/local/mysql/libexec/mysqld: Shutdown complete


解决办法,#export PATH=$PATH:/usr/local/mysql/bin (原因是由于你安装的mysql bin 路径不在默认的搜索path中,导致在启动时无法正确的传送参数给/usr/local/mysql/libexec/mysqld)



     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

以下是在安装centos 系统的时候选择了mysql 安装的时候的配置情况(已经验证OK):

1.路径说明:

   mysql 运行的脚本路径:/usr/bin/

   mysql 语言配置路径: /usr/share

   mysql data dir 路径:/var/lib/mysql

2.

   在已有的数据库中添加一个账号,用于mysqld_multi 管理所有的数据库
   grant shutdown on *.* to identified by 123456';
   grant shutdown on *.* to identified by '123456';
   flush privileges;  

3.shutdown 数据库

4. 把原来的数据库文件夹改名:

   mv mysql mysql1

5. 把现有的数据库复制一份出来:

   cp -pR mysql1 mysql2

6.编辑 /etc/my.cnf

[mysqld_multi]
mysqld=/usr/bin/mysqld_safe
mysqladmin=/usr/bin/mysqladmin
user=multi_admin
password=sf123456


[mysqld1]
port=3307
datadir=/var/lib/mysql1
socket=/var/lib/mysql1/mysql1.sock
pid-file=/var/run/mysqld/mysqld1.pid
log-error=/var/log/mysqld1.log
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

[mysqld2]
port=3308
datadir=/var/lib/mysql2
socket=/var/lib/mysql2/mysql2.sock
pid-file=/var/run/mysqld/mysqld2.pid
log-error=/var/log/mysqld2.log
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

7.使用mysqld_multi 启动实例:

  mysqld_multi start

8. 检查启动情况:

   mysqld_multi report

9.service 情况检查

 [root@ast-est1 log]# service mysqld status
 mysqld (pid 3959 3949) is running...

10.mysqld_multi 启动时的日志在以下位置:

   cat /usr/share/mysqld_multi.log

11. 客户端的连接时,一定要加上h参数,否则无法登陆:

      mysql -uroot -p -P3307 -h127.0.0.1

      mysql -uroot -p -P3308 -h127.0.0.1

12.登陆后检查文件路径是否正确。

     show variables;

13. 完成。

阅读(1058) | 评论(0) | 转发(0) |
0

上一篇:mysql_ping与mysql长连接

下一篇:SQL语句学习

给主人留下些什么吧!~~