多实例Mysql5.5.x源码编译安装
1、 建立Mysql账号
[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -s /sbin/nologin -g mysql -M mysql
useradd参数说明:
-s/sbin/nologin 表示禁止该用户登录
-gmysql 指定属于mysql组
-M表示不创建用户家目录
检查创建的用户:
[[email protected] ~]# tail -1 /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin
2、配置安装环境
安装依赖包:
[[email protected] ~]# yum install gcc gcc-c++ make cmakencurses-devel bison perl lrzsz
建立mysql软件存放目录:(可忽略)
[[email protected] ~]# mkdir /home/tools # mysql软件存放目录
上传mysqla源码包
[[email protected] tools]# ls
mysql-5.5.40.tar.gz
安装目录:
[[email protected] tools]# mkdir /usr/local/mysql –p
[[email protected] tools]#chown –R mysql.mysql/usr/local/mysql
数据目录:
[[email protected] ~]# mkdir /data/{3306,3307}/data –p
[[email protected] ~]#chown –R mysql.mysql /data
[[email protected] ~]# tree /data/
/data #总的多实例根目录
├── 3306 #3306实例的目录
│ └── data #3306实例的数据文件目录
└── 3307 #3307实例的目录
└── data #3307实例的数据文件目录
3、编译安装Mysql
解压:
[[email protected] tools]# tar zxf mysql-5.5.40.tar.gz
配置:
[[email protected] mysql-5.5.40]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
> -DMYSQL_DATADIR= /data/ \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DEXTRA_CHARSETS=all \
> -DENABLED_LOCAL_INFILE=ON \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_FEDERATED_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
> -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
> -DWITH_FAST_MUTEXES=1 \
> -DWITH_ZLIB=bundled \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_READLINE=1 \
> -DWITH_EMBEDDED_SERVER=1 \
> -DWITH_DEBUG=0
静态编译生成mysqld的执行文件:
[[email protected]]# make
安装mysql
[[email protected]]# make install
4、获取mysql多实例的配置文件
[[email protected] mysql-5.5.40]# cpsupport-files/my-innodb-heavy-4G.cnf /data/3306/my.cnf
[[email protected] mysql-5.5.40]# cpsupport-files/my-innodb-heavy-4G.cnf /data/3307/my.cnf
修改配置文件如下:(将会与3307不同的部分)
[[email protected]~]# vim /data/3306/my.cnf
[[email protected] ~]# grep ^[a-z] /data/3306/my.cnf
port =3306
socket =/data/3306/tmp/mysql.sock
port =3306
socket =/data/3306/tmp/mysql.sock
datadir =/data/3306/data
pid-file =/data/3306/mysql.pid
log-bin=/data/3306/mysql-bin
long_query_time = 2
server-id = 1
log-error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysql.pid
与3306实例配置文件不同之处:
[[email protected] ~]# vim /data/3307/my.cnf
[[email protected] ~]# grep ^[a-z] /data/33067/my.cnf
port = 3307
socket = /data/3307/tmp/mysql.sock
port = 3307
socket = /data/3307/tmp/mysql.sock
datadir = /data/3307/data
pid-file = /data/3307/mysql.pid
#log-bin=/data/3307/mysql-bin
#long_query_time= 2
server-id = 3
log-error=/data/3307/mysql_3307.err
pid-file=/data/3307/mysql.pid
5、修改mysql多实例的启动脚本(引用他人脚本)
[[email protected] 3307]# vim/data/3306/mysqld 此脚本cp一份至/data/3307目录下,并更改端口即可
#!/bin/sh
#init
port=3306 /data/3307/mysqld启动脚本端口为3307,此处需更改端口
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"
#startup function
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe--defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
}
#stop function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user}-p${mysql_pwd} -S /data/${port}/tmp/mysql.sock shutdown
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac
两个实例启动脚本比较:
[[email protected] 3307]# diff /data/3306/mysqld/data/3307/mysqld
3c3
< port=3306
---
> port=3307
6、配置mysql全局使用命令:
[[email protected] ~]# echo ‘exportPATH=/usr/local/mysql/bin:$PATH‘ >> /etc/profile
[[email protected] ~]# source /etc/profile
7、mysql数据库启动
a授权
[[email protected] ~]# chown -R mysql.mysql /data/ (此步为新cp进来的musqld授权)
[[email protected] ~]# chmod +x /data/3306/mysqld
[[email protected] ~]# chmod +x /data/3307/mysqld
b初始化数据库
[[email protected] ~]# cd /usr/local/mysql/scripts (和5.1所在的路径不同,不在mysql bin路径下)
[[email protected] scripts]# ./mysql_install_db--basedir=/usr/local/mysql --datadir=/data/3306/data --user=mysql
[[email protected] scripts]# ./mysql_install_db--basedir=/usr/local/mysql --datadir=/data/3307/data --user=mysql
c启动mysql
[[email protected] ~]# /data/3306/mysqld start
Starting MySQL...
[[email protected] ~]# /data/3307/mysqld start
Starting MySQL...
d查看启动结果:
[[email protected] ~]# netstat -ntulp | grep 330
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 18516/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 19193/mysqld
8、登录mysql数据库
[[email protected] ~]# mysql -uroot -P 3306 -S/data/3306/tmp/mysql.sock –p (3307实例更换端口即可,用上述脚本安装,密码为空,直接回车即可登录)
修改mysql登录密码:
[[email protected] ~]# mysqladmin -uroot -S/data/3306/tmp/mysql.sock password q.123456