Linux下搭建MySQL多实例环境
前置条件:安装cmake工具、gcc工具
gcc一般的linux环境都已经安装,现在安装cmake
[root@localhost workspace]# wget http://www.cmake.org/files/v2.8/cmake-2.8.3.tar.gz
[root@localhost workspace]# tar xvf cmake-2.8.3.tar.gz
[root@localhost workspace]# cd cmake-2.8.3
[root@localhost workspace]# ./configure
[root@localhost workspace]# make & make install
下载mysql源码包
[root@localhost workspace]# wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.32.tar.gz
[root@localhost workspace]# tar xvf mysql-5.5.32.tar.gz
[root@localhost workspace]# cd mysql-5.5.32
安装第一个MySQL数据库
(1)创建所需要的文件目录
[root@localhost local]# cd /usr/local/
[root@localhost local]# mkdir mysql
[root@localhost local]# cd mysql/
[root@localhost mysql]# mkdir data
[root@localhost mysql]# mkdir etc
(2)配置MySQL源码编译选项
[root@localhost mysql-5.5.32]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DMYSQL_DATADIR=/usr/local/mysql/data \
> -DSYSCONFDIR=/usr/local/mysql/etc \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_MEMORY_STORAGE_ENGINE=1 \
> -DWITH_READLINE=1 \
> -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
> -DMYSQL_TCP_PORT=3306 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DEXTRA_CHARSETS=all \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
(3)编译安装
[root@localhost mysql-5.5.32]# make & make install
(4)配置第一个MySQL实例
[root@localhost mysql-5.5.32]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql .
[root@localhost mysql]# cp support-files/my-medium.cnf /usr/local/mysql/etc/my.cnf
[root@localhost mysql]# vi /usr/local/mysql/etc/my.cnf
添加 datadir=/usr/local/mysql/data
default-storage-engine=MyISAM
[root@localhost mysql]# cd /usr/local/mysql/scripts/
[root@localhost scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &
[root@localhost bin]# cd /usr/local/mysql/bin
[root@localhost bin]# ./mysqld_safe --user=root
[1] 28869
[root@localhost bin]# 131016 20:07:13 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
131016 20:07:14 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
查看服务是否启动成功
[root@localhost bin]# netstat -tlnap | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 29147/mysqld
登录MySQL并修改root用户密码
[root@localhost bin]# ./mysqladmin -uroot password 'eisoo.com'
[root@localhost bin]# ./mysql -uroot -peisoo.com
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
安装第二个MySQL数据库
(1)清理配置信息
[root@localhost bin]# cd /workspace/mysql-5.5.32
[root@localhost mysql-5.5.32]# make clean
[root@localhost mysql-5.5.32]# rm -rf CMakeCache.txt
(2)创建所需要的文件目录
[root@localhost mysql-5.5.32]# cd /usr/local/
[root@localhost local]# mkdir mysql3307
[root@localhost local]# cd mysql3307/
[root@localhost mysql3307]# mkdir data
[root@localhost mysql3307]# mkdir etc
(3)配置第二个实例的编译信息
[root@localhost bin]# cd /workspace/mysql-5.5.32
[root@localhost mysql-5.5.32]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql3307 \
> -DMYSQL_DATADIR=/usr/local/mysql3307/data \
> -DSYSCONFDIR=/usr/local/mysql3307/etc \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_MEMORY_STORAGE_ENGINE=1 \
> -DWITH_READLINE=1 \
> -DMYSQL_UNIX_ADDR=/tmp/mysqld3307.sock \
> -DMYSQL_TCP_PORT=3307 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DEXTRA_CHARSETS=all \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
(4)编译安装
[root@localhost mysql-5.5.32]# make & make install
(5)配置第二个MySQL实例
[root@localhost mysql-5.5.32]# cd /usr/local/mysql3307
[root@localhost mysql3307]# chown -R mysql:mysql .
[root@localhost mysql3307]# cp support-files/my-medium.cnf /usr/local/mysql3307/etc/my.cnf
[root@localhost mysql3307]# vi /usr/local/mysql3307/etc/my.cnf
添加 datadir=/usr/local/mysql3307/data
default-storage-engine=MyISAM
[root@localhost mysql3307]# cd /usr/local/mysql3307/scripts/
[root@localhost scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql3307 --datadir=/usr/local/mysql3307/data &
[root@localhost scripts]# cd /usr/local/mysql3307/bin
[root@localhost bin]# ./mysqld_safe --user=root
131016 20:40:27 mysqld_safe Logging to '/usr/local/mysql3307/data/localhost.localdomain.err'.
131016 20:40:27 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql3307/data
查看服务是否启动成功
[root@localhost bin]# netstat -tlnap | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 29147/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 7447/mysqld
登录MySQL并修改root用户密码
[root@localhost bin]# ./mysqladmin -uroot password 'eisoo.com123'
[root@localhost bin]# ./mysql -uroot -peisoo.com123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
增加系统环境变量
[root@localhost /]# vi /etc/profile
增加一行:export PATH=/usr/local/mysql/bin:/usr/local/mysql3307/bin:$PATH
[root@localhost /]# source /etc/profile
[root@localhost /]# export $PATH
分别启动不同实例:
[root@localhost /]# mysqld_safe --user=root --port=3306 --socket=/tmp/mysqld.sock --datadir=/usr/local/mysql/data &
[root@localhost /]# mysqld_safe --user=root --port=3307 --socket=/tmp/mysqld3307.sock --datadir=/usr/local/mysql3307/data &
分别登陆不同实例:
[root@localhost /]# mysql -uroot -peisoo.com -S /tmp/mysqld.sock
[root@localhost /]# mysql -uroot -peisoo.com123 -S /tmp/mysqld3307.sock
启动还是太麻烦,可以这样做:
[root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql3306
[root@localhost /]# cp /usr/local/mysql3307/support-files/mysql.server /etc/init.d/mysql3307
给mysql用户添加权限:
[root@localhost /]# chmod -R 755 /usr/local/mysql/data
[root@localhost /]# chmod -R 755 /usr/local/mysql/data
分别启动实例对应的服务:
[root@localhost tmp]# service mysql3306 start
Starting MySQL. [确定]
[root@localhost tmp]# service mysql3307 start
Starting MySQL. [确定]
查看服务:
[root@localhost tmp]# netstat -tlnap | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 10759/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 11097/mysqld
将服务添加到系统服务中,使其开机自动启动。
[root@localhost /]# chkconfig --add mysql3306
[root@localhost /]# chkconfig --add mysql3307
ps:在linux下可以启动n个mysql实例,如果两个不够,按照以上步骤可以继续安装,目前本人只会用源码包搭建,如果那位大神知道用rpm包在Red Hat Linux下搭建MySQL多实例环境,请多多指教!谢谢!