- 系统环境
- yum安装cmake
- 开始安装MySQL
- 建立MySQL用户账号
- 获取MySQL软件包
- 创建MySQL多实例的数据文件目录
- 创建MySQL多实例的配置文件
- 添加配置文件内容
- 创建MySQL多实例的启动文件
- 配置MySQL多实例的文件权限
- 设置环境变量
- 多实例的登录方式需要指定sock的。
- 配置及管理MySQL多实例数据库
系统环境
[root@db02 ~]# uname -a Linux db02 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
yum安装cmake
yum install -y cmake
开始安装MySQL
大型公司一般都会将MySQL软件定制成rpm包,然后放到yum仓库里,使用yum安装,中小企业里的二进制和编译安装的区别不大。
建立MySQL用户账号
首先以root身份登录到Linux系统中,然后执行如下命令创建mysql用户账号:
useradd -s /sbin/nologin -M mysql
id mysql
#根据上述输出结果,可以看到mysql用户和组已经成功创建。
获取MySQL软件包
MySQL软件包的下载地址为:http://dev.mysql.com/downloads/mysql/。
可以把软件下载到客户端计算机本地后使用rz等工具传到Linux里,或者找到网络下载地址后直接在Linux里使用wget下载。
提示:本例以MySQL编译的方式来讲解,在生产场景中,二进制和源码包两种安装方法都是可以用的,其应用场景一般没什么差别。不同之处在于,二进制的安装包较大,名字和源码包也有些区别,二进制安装过程比源码更快。
mkdir /home/oldboy/tools -p
cd /home/oldboy/tools/
#下载MySQL源码包
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.32.tar.gz
tar xf mysql-5.5.32.tar.gz
cd mysql-5.5.32
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_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
# 编译安装
make &&make install &&cd ..
提示,编译时可配置的选项很多,具体可参考官方文档
创建MySQL多实例的数据文件目录
在企业中,通常以/data目录作为MySQL多实例总的根目录,然后规划不同的数字(即MySQL实例端口号)作为/data下面的二级目录,不同的二级目录对应的数字就作为MySQL实例的端口号,以区别不同的实例,数字对应的二级目录下包含MySQL的数据文件、配置文件及启动文件等。
下面以配置3306、3307两个实例为例进行讲解。创建MySQL多实例的目录如下:
[root@db02 tools]# mkdir -p /data/{3306,3307}/data
[root@db02 tools]# tree /data/
/data/
├── 3306
│ └── data
└── 3307
└── data
下面为MySQL安装路径设置不带版本号的软链接/application/mysql,操作步骤如下:
[root@db02 tools]# ln -s /application/mysql-5.5.32/ /application/mysql
#补充:如果系统里有我们曾经讲解过的单实例安装的数据库文件和启动程序,最好停掉或删除,以免产生冲突
[root@db02 tools]# ls /application/mysql
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
如果上述操作未出现错误,查看/application/mysql/目录下有内容,则MySQL5.5.32源代码包采用cmake方式的安装就算成功了。
创建MySQL多实例的配置文件
MySQL数据库默认为用户提供了多个配置文件模板,用户可以根据服务器硬件配置的大小来选择。
[root@db02 tools]# cd /application/mysql-5.5.32/
[root@db02 mysql-5.5.32]# ls -l support-files/my*.cnf
-rw-r--r-- 1 root root 4759 3月 17 12:05 support-files/my-huge.cnf
-rw-r--r-- 1 root root 19809 3月 17 12:05 support-files/my-innodb-heavy-4G.cnf
-rw-r--r-- 1 root root 4733 3月 17 12:05 support-files/my-large.cnf
-rw-r--r-- 1 root root 4744 3月 17 12:05 support-files/my-medium.cnf
-rw-r--r-- 1 root root 2908 3月 17 12:05 support-files/my-small.cnf
注意: 1)关于mysql my.cnf中参数的调优,由于篇幅关系不会详细介绍,有需要的读者请参考老男孩老师的相关课程。(因为我是老男孩老师的学生)
2)support-files下有mysql my.cnf的各种配置样例,里面的注释非常详细,不过是英文的。
上面是单实例的默认配置文件模板,如果配置多实例,和单实例会有不同。为了让MySQL多实例之间彼此独立,要为每一个实例建立一个my.cnf配置文件和一个启动文件MySQL,让它们分别对应自己的数据文件目录data。
添加配置文件内容
首先,通过vim命令添加配置文件内容,命令如下: vim /data/3306/my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
#binlog_format = "ROW"
character-set-server=utf8
user = 'mysql'
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
open_files_limit = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-OOMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
log-slow-queries = /data/3306/slow.log
log_queries_not_using_indexes
pid-file = /data/3306/mysql.pid
relay-log = /data/3306/relay-bin
log-bin = /data/3306/mysql-bin
slow_query_log
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1M
general_log = on
general_log_file = /data/3306/data/MySQL_oldboy.log
max_binlog_cache_size = 1M
max_binlog_size = 2M
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 1
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 2M
[mysqld_safe]
log-error=/data/3306/mysql.err
pid-file=/data/3306/mysqld.pid
vim /data/3307/my.cnf
[client]
port = 3307
socket = /data/3307/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
#binlog_format = "ROW"
character-set-server=utf8
user = 'mysql'
port = 3307
socket = /data/3307/mysql.sock
basedir = /application/mysql
datadir = /data/3307/data
open_files_limit = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-OOMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
log-slow-queries = /data/3307/slow.log
log_queries_not_using_indexes
pid-file = /data/3307/mysql.pid
relay-log = /data/3307/relay-bin
log-bin = /data/3307/mysql-bin
slow_query_log
relay-log-info-file = /data/3307/relay-log.info
binlog_cache_size = 1M
general_log = on
general_log_file = /data/3307/data/MySQL_oldboy.log
max_binlog_cache_size = 1M
max_binlog_size = 2M
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 3
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 2M
[mysqld_safe]
log-error=/data/3307/mysql.err
pid-file=/data/3307/mysqld.pid
最终完成后的多实例根/data目录结果如下:
[root@db02 mysql-5.5.32]# tree /data/
/data
|--3306
| |-- data
| `-- my.cnf#这个就是3306实例的配置文件
`--3307
|-- data
`-- my.cnf#这个就是3307实例的配置文件
4 directories,2 files
有关配置文件的参数说明,可以根据my-innodb-heavy-4G.cnf或查找相关资料,进行完整注释。
不同的实例需要添加的my.cnf内容会有区别,其中的配置由官方的配置模板修改而来。当然,在实际工作中,我们是拿早已配置好的模板来进行修改的,可以通过rz等方式上传配置文件模板my.cnf文件到相关目录下。
创建MySQL多实例的启动文件
MySQL多实例启动文件的创建和配置文件的创建几乎一样,也可以通过vim命令来添加,如下:
vim /data/3306/mysql
#!/bin/bash
#################################################
#this scripts is created by anonymous at 2017-3-20
#################################################
#init
port=3306
mysql_user="root"
mysql_pwd="oldboy123" #这里将来要修改为和数据库密码一致
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#resrtart 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
vim /data/3307/mysql
#!/bin/bash
#################################################
#this scripts is created by anonymous at 2017-3-20
#################################################
#init
port=3307
mysql_user="root"
mysql_pwd="oldboy456" #这里将来要修改为和数据库密码一致
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#resrtart 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
当然,在实际工作中我们是拿早已配置好的模板来进行修改的,可以通过rz等方式上传配置文件模板MySQL文件到相关目录下。
最终完成后的多实例根/data目录结果如下:
[root@db02 mysql-5.5.32]# tree /data/
/data
|--3306
| |-- my.cnf #<==3306实例的配置文件
| `-- mysql #<==3306实例的启动文件
`--3307
|-- my.cnf #<==3307实例的配置文件
`-- mysql #<==3307实例的启动文件
2 directories,4 files
配置MySQL多实例的文件权限
通过下面的命令,授权MySQL多实例所有启动文件的MySQL可执行,设置700权限最佳,注意不要755权限,因为启动文件里有数据库管理密码,会被读取到。
[root@db02 home]# find /data/ -type f -name "mysql"|xargs chmod +x
[root@db02 home]# find /data/ -type f -name "mysql"|xargs ls -l
-rwxr-xr-x 1 root root 1300 3月 17 15:40 /data/3306/mysql
-rwxr-xr-x 1 root root 1299 3月 17 15:40 /data/3307/mysql
通过下面的命令。授权MySQL用户和组管理整个多实例的根目录/data
[root@db02 home]# chown -R mysql.mysql /data
[root@db02 home]# find /data/ -name mysql |xargs ls -l
-rwxr-xr-x 1 mysql mysql 1300 3月 17 15:40 /data/3306/mysql
-rwxr-xr-x 1 mysql mysql 1299 3月 17 15:40 /data/3307/mysql
初始化数据库就是加载MySQL自身的库和表
[root@db02 home]# cd /application/mysql/scripts/
[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3306/data/ --user=mysql
[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
#看到有两个ok 就对了
#WARNING: The host 'db02' could not be looked up with resolveip. 出现这个表示我们的主机名没做解析。
#[root@db02 scripts]# vi /etc/hosts
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 localhost db02
[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
设置环境变量
[root@db02 3307]# cp /application/mysql/bin/* /usr/local/sbin/
需要特别说明一下,在多实例启动文件中,启动MySQL不同实例服务,所执行的命令实质是有区别的,例如,
启动3306实例的命令如下:
mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
启动3307实例的命令如下:
mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
下面看看在多实例启动文件中,停止MySQL不同实例服务的实质命令。 停止3306实例的命令如下:
mysqladmin -u root -poldboy123 -S /data/3306/mysql.sock shutdown
停止3307实例的命令如下:
mysqladmin -u root -poldboy456 -S /data/3307/mysql.sock shutdown
多实例的登录方式需要指定sock的。
[root@db02 3307]# mysql -S /data/3306/mysql.sock #现在可以进到3306啦
[root@db02 3307]# mysql -S /data/3307/mysql.sock #现在可以进到3307啦
[root@db02 ~]# mysqladmin -S /data/3306/mysql.sock password ‘oldboy123’ [root@db02 ~]# mysqladmin -S /data/3307/mysql.sock password ‘oldboy456’
至此 编译安装完毕
配置及管理MySQL多实例数据库
1.配置MySQL多实例数据库开机自启动 服务的开机自启动很关键,MySQL多实例的启动也不例外,把MySQL多实例的启动命令加入/etc/rc.local,实现开机自启动,命令如下:
echo "#mysql multi instances" >>/etc/rc.local
echo "/data/3306/mysql start" >>/etc/rc.local
echo "/data/3307/mysql start" >>/etc/rc.local
tail -3 /etc/rc.local
提示:要确保MySQL脚本可执行呦!
(END)
Keegv| 陈宝佳 作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。