转载



实践MySQL(mysqld)多实例/多版本同时运行





摘要:

在RedHat 9.0 (2.4.20-8 )中,实践MySQL的

(1) 单版本多实例,

(2) 多版本单实例运行

(3) 多版本多实例运行。



实现目标:

1) 同一版本的多个实例同时运行。

两个4.1.9的实例分别于3301,3302端口监听并提供服务。



2) 不同版本的单个实例同时运行。

4.1.9和5.0.2的实例分别于3304,3305端口监听并提供服务。



3) 不同版本的多个实例同时运行。

4.1.9的2个实例分别于3301,3302端口监听并提供服务。

5.0.2的2个实例分别于3308,3309端口监听并提供服务。





必要准备:

1) 运行环境:



[root@trydofor]#cat /proc/version

Linux version 2.4.20-8 (bhcompile@porky.devel.redhat.com)(gcc version

3.2.2 20030222(Red Hat Linux 3.2.2-5))#1 Thu Mar 13 17:54:28 EST 2003



[root@trydofor]#ls -l |grep mysql|awk '{print $5,$9}'

33368998 mysql-max-5.0.2-alpha-pc-linux-i686.tar.gz

28195102 mysql-standard-4.1.9-pc-linux-gnu-i686.tar.gz



[注] 获得mysql二进制包



2) 理论基础:



mysqld 读取配置文件的顺序:

1./etc/my.cnf, Global options.

2.$DATADIR/my.cnf, Server-specific options.

3.defaults-extra-file,

The file specified with --defaults-extra-file=path, if any

4.~/.my.cnf, User-specific options.



mysqld 多进程运行的必要条件:

至少要使 pid-file, datadir, socket, port 是独立分开。



实现策略:



1) 安装与准备

[注] 添加mysql组和用户

# groupadd mysql

# useradd -g mysql mysql

[注] 解包到/usr/local

# tar -xzf mysql-standard-4.1.9-pc-linux-gnu-i686.tar.gz -C /usr/local

# tar -xzf mysql-max-5.0.2-alpha-pc-linux-i686.tar.gz -C /usr/local

[注] 建立软链接,方便操作

# cd /usr/local

# ln -s mysql-standard-4.1.9-pc-linux-gnu-i686 mysql4

# ln -s mysql-max-5.0.2-alpha-pc-linux-i686 mysql5

[注] 初始化data,更改文件属性

# cd mysql4

# scripts/mysql_install_db --user=mysql

# chown -R root .

# chown -R mysql data

# chgrp -R mysql .

[注] 复制data,以供单版本多实例分配

cp -a data data_1

cp -a data data_2

[注] 初始化data,更改文件属性

# cd ../mysql5

# scripts/mysql_install_db --user=mysql

# chown -R root .

# chown -R mysql data

# chgrp -R mysql .

[注] 复制data,以供单版本多实例分配

cp -a data data_1

cp -a data data_2



2) 配置与测试



2.1) 单版本多实例的实现



[注] 本目标利用 $MYSQL/bin/mysqld_multi 脚本来实现。

其实,通过启动多个 $MYSQL/bin/mysqld_safe 脚本也实现,

不过 mysqld_multi 提供了更方便更简单的途径,不用有点可惜:)



2.1.1) 配置mysql



#pwd

/usr/local/mysql4

#cat >my_multi.cnf

[mysqld_multi]

mysqld = /usr/local/mysql4/bin/mysqld_safe

mysqladmin = /usr/local/mysql4/bin/mysqladmin

user = root



[mysqld1]

socket = /tmp/mysql_4_1.sock

port = 3301

pid-file = /usr/local/mysql4/data_1/hostname.pid

datadir = /usr/local/mysql4/data_1

log = /usr/local/mysql4/data_1/hostname.log

user = mysql



[mysqld2]

socket = /tmp/mysql_4_2.sock

port = 3302

pid-file = /usr/local/mysql4/data_2/hostname.pid

datadir = /usr/local/mysql4/data_2

log = /usr/local/mysql4/data_2/hostname.log

user = mysql



CTRL+D



2.1.2) 启动mysql



#pwd

/usr/local/mysql4

[注] 启动mysqld2个实例 (GNR1和GNR2)

# bin/mysqld_multi --config-file=./my_multi.cnf start 1,2

Starting mysqld daemon with databases from /usr/local/mysql4/data_1

Starting mysqld daemon with databases from /usr/local/mysql4/data_2



[注] 查看端口是否被监听

# netstat -lt |grep 33

tcp 0 0 *:3301 *:* LISTEN

tcp 0 0 *:3302 *:* LISTEN



2.1.3) 测试mysql



#pwd

/usr/local/mysql4

[注] 通过mysql_4_1.sock连接3001端口的mysql(GNR1)

# bin/mysql -u root -S /tmp/mysql_4_1.sock -e "SELECT @@VERSION;"

[注] 停止3001端口的mysql(GNR1)

# bin/mysqld_multi --config-file=./my_multi.cnf stop 1

STOPPING server from pid file /usr/local/mysql4/data_1/hostname.pid

050223 08:56:04 mysqld ended



[注] 再次连接(GNR4),系统会提示错误

# bin/mysql -u root -S /tmp/mysql_4_1.sock -e "SELECT @@VERSION;"

ERROR 2002 (HY000):

Can't connect to local MySQL server through socket '/tmp/mysql_4_1.sock' (2)



[注] 相同的步骤测试3002端口的mysql(GNR2),输出与上相同

# bin/mysql -u root -S /tmp/mysql_4_2.sock -e "SELECT @@VERSION;"

# bin/mysqld_multi --config-file=./my_multi.cnf stop 2

# bin/mysql -u root -S /tmp/mysql_4_2.sock -e "SELECT @@VERSION;"



2.2) 多版本单实例的实现



[注] 本目标利用启动多个 $MYSQL/bin/mysqld_safe 脚本实现



2.2.1) 配置mysql



[注] 在mysql4/data目录中添加配置文件my.cnf (也可以通过命令行参数实现)



#pwd

/usr/local/mysql4

#cat >data/my.cnf

[mysqld]

socket = /tmp/mysql_4_4.sock

port = 3304

basedir = /usr/local/mysql4

default-character-set = gbk



CTRL+D



[注] 在mysql5/data目录中添加配置文件my.cnf (也可以通过命令行参数实现)



#pwd

/usr/local/mysql5

#cat >data/my.cnf

[mysqld]

socket = /tmp/mysql_5_5.sock

port = 3305

basedir = /usr/local/mysql5

default-character-set = gbk



CTRL+D



2.2.2) 启动与测试



[注] 分别启动mysql4和mysql5



# cd /usr/local/mysql4

# bin/mysqld_safe --user=mysql --pid-file=data/hostname.pid --datadir=data &

# cd /usr/local/mysql5

# bin/mysqld_safe --user=mysql --pid-file=data/hostname.pid --datadir=data &



[注] 查看端口是否被监听

# netstat -lp|grep 33

tcp 0 0 *:3304 *:* LISTEN

tcp 0 0 *:3305 *:* LISTEN



[注] 查看mysql版本

# bin/mysql -u root -S /tmp/mysql_4_4.sock -e "SELECT @@VERSION;"

# bin/mysql -u root -S /tmp/mysql_5_5.sock -e "SELECT @@VERSION;"



[注] 检测mysql5

# bin/mysql -u root -S /tmp/mysql_5_5.sock

[注] 利用文末 注意事项 4)的sql语句可以检测msyql5版本



[注] 停止mysql

# bin/mysqladmin -u root -S /tmp/mysql_4_4.sock shutdown

# bin/mysqladmin -u root -S /tmp/mysql_5_5.sock shutdown



2.3) 多版本多实例的实现



[注] 利用启动多个 $MYSQL/bin/mysqld_multi 脚本实现,其方法同目标1。



2.3.1) 配置mysql



[注] 仿造目标1)方法,创建mysql5的配置

#pwd

/usr/local/mysql5

#cat >my_multi.cnf

[mysqld_multi]

mysqld = /usr/local/mysql5/bin/mysqld_safe

mysqladmin = /usr/local/mysql5/bin/mysqladmin

user = root



[mysqld1]

socket = /tmp/mysql_5_8.sock

port = 3308

pid-file = /usr/local/mysql5/data_1/hostname.pid

datadir = /usr/local/mysql5/data_1

log = /usr/local/mysql5/data_1/hostname.log

user = mysql



[mysqld2]

socket = /tmp/mysql_5_9.sock

port = 3309

pid-file = /usr/local/mysql5/data_2/hostname.pid

datadir = /usr/local/mysql5/data_2

log = /usr/local/mysql5/data_2/hostname.log

user = mysql



CTRL+D



2.3.2) 启动mysql



#cd /usr/local/mysql4

# bin/mysqld_multi --config-file=./my_multi.cnf start 1,2

Starting mysqld daemon with databases from /usr/local/mysql4/data_1

Starting mysqld daemon with databases from /usr/local/mysql4/data_2



# netstat -lt |grep 33

tcp 0 0 *:3301 *:* LISTEN

tcp 0 0 *:3302 *:* LISTEN

tcp 0 0 *:3308 *:* LISTEN

tcp 0 0 *:3309 *:* LISTEN



2.3.3) 测试mysql



# bin/mysql -u root -S /tmp/mysql_4_1.sock -e "SELECT @@VERSION;"

# bin/mysql -u root -S /tmp/mysql_4_1.sock -e "SELECT @@VERSION;"

# bin/mysql -u root -S /tmp/mysql_5_8.sock -e "SELECT @@VERSION;"

# bin/mysql -u root -S /tmp/mysql_5_9.sock -e "SELECT @@VERSION;"



[注] 利用文末 注意事项 4)的sql语句可以检测msyql5版本

# bin/mysql -u root -S /tmp/mysql_5_8.sock





注意事项:



1) 系统环境变量的设定

很多linux发行版都把/usr/local/mysql加入了$PATH,同时/usr/bin/下

还有很多mysql命令和脚本。如果不注意,可能启动了错误版本的命令。



2) $MYSQL/scripts/mysql_install_db 初始化不成功

这时要停掉系统中运行的mysqld,否则就要修改mysql_install_db

才能成功运行。---- 我是没有成功修改过



3) $MYSQL/bin/mysqld_multi 启动多本版可能不正确

即,把msyql4和mysql5写在一个配置文件指定不同的中通过一个mysqld_multi启动。



[mysqld_multi]

user = root

[mysqld4]

socket = /tmp/mysql_4_4.sock

port = 3304

pid-file = /usr/local/mysql4/data/hostname.pid

datadir = /usr/local/mysql4/data

log = /usr/local/mysql4/data/hostname.log

user = mysql

[mysqld5]

socket = /tmp/mysql_5_5.sock

port = 3305

pid-file = /usr/local/mysql5/data/hostname.pid

datadir = /usr/local/mysql5/data

log = /usr/local/mysql5/data/hostname.log

user = mysql



当用mysql5中的mysqld_multi启动时,查看mysql版本都显示5的信息

使用4)中的语句测试,都不通过。原因尚在调查中...



4) 下面语句在mysql5上可以成功通过(主要是最后一句)

use test;

drop table if exists t3;

create table t3 ( userid bigint(20) not null default 0 );

drop procedure if exists bug8116;

create procedure bug8116(in _userid int)

select * from t3 where userid = _userid;

call bug8116(42);