###一:准备环境:
安装MySQL的依赖包并检查是否安装完成
```
yum?-y?install??ncurses-devel?libaio-devel?cmake
rpm?-qa??ncurses-devel?libaio-devel?cmake
```
(2)创建启动MySQL的用户
```
useradd?-s?/sbin/nologin?-M?mysql
```
(3)规范软件包安装路径,并下载解压mysql-5.5.32源码包
```
mkdir?/home/oldboy/tools
cd?/home/oldboy/tools
wget?ftp://10.0.0.1/mysql-5.5.32.tar.gz
tar?xf?mysql-5.5.32.tar.gz?
cd?mysql-5.5.32
```
###二:编译安装MySQL源码包
(1)使用cmake命令对MySQL的源码包进行编译安装
```
cmake?-DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32?\
-DMYSQL_DATADIR=/application/mysql-5.5.32/data?\
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock?\
-DDEFAULT_CHARSET=utf8?\
-DDEFAULT_COLLATION=utf8_general_ci?\
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii?\
-DENABLED_LOOCAL_INFILE=ON?\
-DWITH_INNOBASE_STORAGE_ENGINE=1?\
-DWITH_FEDERATED_STORAGE_ENGINE=1?\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1?\
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1?\
-DWITHOUT_PARRTITION_STORAGE_ENGINE=1?\
-DWITH_FAST_MUTEXES=1?\
-DWITH_ZLIB=bundled?\
-DENABLED_LOCAL_INFILE=1?\
-DWITH_READLINE=1?\
-DWITH_EMBEDDED_SERVER=1\
-DWITH_DEBUG=0
echo?$?
```
(2)生成Makefile文件的步骤接下来进行安装
```shell
make?&&?make?install
echo?$?
```
(3)到了此步我们的MySQL数据库就安装完成了
###三:配置MySQL数据库
(1)为MySQL安装目录创建软链接
```shell
ln?-s?/application/mysql-5.5.32?/application/mysql
ll?/application/
```
(2)创建MySQL的多实例目录
```shell
mkdir?-p?/data/{3306,3307}/data
```
(3)下载MySQL多实例的/data
```shell
cd?/home/oldboy/tools/
wget?ftp://10.0.0.1/data.zip
unzip?data.zip
cp?-r?data?/
tree?/data/
```
(4)设置多实例MySQL启动脚本,允许执行
```shell
find?/data?-type?f?-name?"mysql"|xargs?chmod?+x
find?/data?-type?f?-name?"mysql"|xargs?ls?-l
```
(5)对MySQL数据库的数据目录进行授权管理
```shell
chown?-R?mysql.mysql?/data/
ll?/data/
```
(6)对MySQL数据库进行初始化操作
```shell
cd?/application/mysql/scripts/
./mysql_install_db?--basedir=/application/mysql/?--datadir=/data/3306/data/?--user=mysql
./mysql_install_db?--basedir=/application/mysql/?--datadir=/data/3307/data/?--user=mysql
```
###四:MySQL多实例配置及启动
(1)启动第一个MySQL实例
```shell
/data/3306/mysql?start
```
(2)启动第二个MySQL实例
```shell
/data/3307/mysql?start
```
(3)启动完成进行检查
```shell
netstat?-lntup|grep?330
```
(4)将MySQL命令复制到指定的sock目录
```shell
cp?/application/mysql/bin/*?/usr/local/sbin/
```
(5)使用mysql命令登录数据库,登录的时候需要指定sock
```shell
mysql?-uroot?-S?/data/3306/mysql.sock
mysql?-uroot?-S?/data/3307/mysql.sock
```
(6)设置MySQL多实例的数据库开机自启动
```shell
echo?"mysql?multi?instances"?>>/etc/rc.local
echo?"/data/3306/mysql?start"?>>/etc/rc.local
echo?"/data/3307/mysql?start"?>>/etc/rc.local
tail?-n?3?/etc/rc.local
```
常见错误问题排除:
(1)是否对主机名做解析
(2)tmp目录的权限是否为1777
(3)如果启动MySQL多实例没有查到端口就稍等一会
(4)通过日志.err结尾的日志进行排错
###五:MySQL添加多实例的方法
(1)创建一个多实例的存放目录
```shell
mkdir?/data/3308/data?-p
```
(2)复制一份实例到指定的目录
```shell
\cp?/data/3306/{my.cnf,mysql}?/data/3308/
ll?/data/3308
```
(3)修改配置文件之前一定要备份
```shell
\cp?/data/3308/{my.cnf,my.cnf.source.bak}
\cp?/data/3308/{mysql,mysql.source.bak}
ll?/data/3308/
```
(4)使用sed命令批量更改MySQL的配置文件与启动脚本
```shell
sed?-i?‘s/3306/3308/g‘?/data/3308/{my.cnf,mysql}
sed?-i?‘s/server-id?=?1/server-id?=?8/g‘?/data/3308/my.cnf
```
(5)设置3308数据库实例所有者为mysql用户
```shell
chown?-R?mysql.mysql?/data/3308/
ll?/data/3308/
```
(6)设置第三个数据库实例启动脚本的权限为700
```shell
chmod?700?/data/3308/mysql
ll?/data/3308/mysql
```
(7)对第三个数据库实例进行初始化数据库操作
```shell
cd?/application/mysql/scripts/
./mysql_install_db?--basedir=/application/mysql/?--datadir=/data/3308/data/?--user=mysql
```
(8)授权mysql启动脚本具有执行权限
```shell
chmod?+x?/data/3308/mysql
```
(9)启动第三个实例的数据库
```shell
/data/3308/mysql?start
```
(10)启动完成进行检查
```shell
netstat?-lntup|grep?330
```
(11)测试是否可以进入数据库
```shell
mysql?-S?/data/3308/mysql.sock
```
(12)将数据库设置开机自启动
```shell
echo?"/data/3308/mysql?start"?>>/etc/rc.local
tail?-1?/etc/rc.local
```
###六:因为脚本里面设置的密码是oldboy?所以如果我们不设置将无法停止MySQL
```shell
mysqladmin?password?oldboy?-S?/data/3306/mysql.sock
mysqladmin?password?oldboy?-S?/data/3307/mysql.sock
mysqladmin?password?oldboy?-S?/data/3308/mysql.sock
mysql?-uroot?-poldboy123?-S?/data/3306/mysql.sock
```
###七:MySQL远程连接测试
(1)命令使用格式
```shell
mysql?-uroot?-poldboy?-h?10.0.0.52?-P?端口号?-P?为端口参数
```
(2)远程连接多实例数据库测试
```shell
mysql?-uroot?-poldboy?-h?10.0.0.52?-P?3306
```