设为首页 收藏本站
查看: 665|回复: 0

[经验分享] MySQL5.5多实例编译安装——多配置文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-12 09:15:59 | 显示全部楼层 |阅读模式
一、什么是MySQL多实例?
MySQL多实例简单的说就是在一台服务器上安装一套MySQL程序,通过不同的端口对外提供访问,多实例不仅节省物理主机成本,还有效提升了单台物理主机的CPU、磁盘I/O使用效率,而且还可以在多实例之间做部署数据库HA方案。
二、如何配置MySQL多实例?
配置mysql多实例有两种方式
1、根据官方提供的是通过mysqld_multi使用单独的配置文件来实现多实例,这种方式定制每个实例的配置不太方面,优点是管理起来很方便,集中管理。
2、使用多个配置文件和启动文件,配置文件之间的区别:server-id、socket文件的位置、配置路径和数据存放位置不同。初始化的时候只用不同的配置文件进行初始化数据库,启动时使用不同的启动文件来启动,这种方法逻辑和配置简单,但是不方便管理。
下面我们以第二种多实例的方法进行配置
三、多实例配置
MySQL安装的是mysql5.5.52版本,安装方法请看MySQL5.5.52编译安装
1、停止单实例mysql数据库

1
2
[iyunv@db01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!



2、禁止开机自启动

1
2
3
[iyunv@db01 ~]# chkconfig mysqld off
[iyunv@db01 ~]# chkconfig --list mysqld
mysqld          0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭6:关闭



3、创建多实例根目录/data/目录

1
[iyunv@db01 ~]# mkdir -p /data/{3306,3307}/data



需要特别说明一下,在多实例启动文件中,启动MySQL不同势力服务所需要执行的命令实质是有区别的,例如,启动3306实例命令如下

1
mysql_safe --defaults-file=/data/3306/mysql &>/dev/null



启动3307实例的命令如下:

1
mysql_safe --defaults-file=/data/3307/mysql &>/dev/null



下面看看多实例启动文件中,停止MySQL不同实例服务的实质命令
停止3306实例的命令如下:

1
mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown



停止3307实例的命令如下:

1
mysqladmin -uroot -p123456 -S /data/3307/mysql.sock shutdown



4、创建MySQL多实例的配置文件和启动文件
1)3306mysql实例配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[iyunv@db01 ~]# vim /data/3306/my.cnf
[client]
port            = 3306
socket          = /data/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
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-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
#log-error = /data/3306/error.log
#log-slow-queries = /data/3306/slow.log
pid-file = /data/3306/mysql.pid
log-bin = /data/3306/mysql-bin
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
#myisam_sort_buffer_size = 1M
#myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover
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_3306.err
pid-file=/data/3306/mysqld.pid



2)3307mysql实例配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
[iyunv@db01 ~]# cp /data/3306/my.cnf /data/3307/my.cnf
[iyunv@db01 ~]# sed -i 's#3306#3307#g' /data/3307/my.cnf
[iyunv@db01 ~]# sed -n /server-id/p /data/3307/my.cnf
server-id = 1
[iyunv@db01 ~]# sed -i 's#server-id = 1#server-id = 2#g' /data/3307/my.cnf                       
[iyunv@db01 ~]# cat /data/3307/my.cnf
[client]
port            = 3307
socket          = /data/3307/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
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-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
#log-error = /data/3307/error.log
#log-slow-queries = /data/3307/slow.log
pid-file = /data/3307/mysql.pid
log-bin = /data/3307/mysql-bin
relay-log = /data/3307/relay-bin
relay-log-info-file = /data/3307/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
#myisam_sort_buffer_size = 1M
#myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 2
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_3307.err
pid-file=/data/3307/mysqld.pid



5、MySQL多实例启动文件的创建和配置文件创建几乎一样,也可以通过vim命令来添加如下:
1)3306mysql实例启动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[iyunv@db01 ~]# vim /data/3306/mysql
#!/bin/bash
################################################
# Filename:mysql
# Description:Start MySQL multi instance script
# Version:1.0
# Date:2016/12/10
# Author:xuanwiei
# Email:1756112532@qq.com
################################################
#init
port=3306
mysql_user="root"
mysql_pwd="123456"  #这里将来是要修改为和数据库密码一致
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
}
#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



2)3307mysql实例启动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[iyunv@db01 ~]# cp /data/3306/mysql /data/3307/mysql
[iyunv@db01 ~]# sed -i 's#3306#3307#g' /data/3307/mysql
[iyunv@db01 ~]# cat /data/3307/mysql
#!/bin/bash
################################################
# Filename:    mysql
# Description: Start MySQL multi instance script
# Version:     1.0
# Date:        2016/12/10
# Author:      xuanwiei
# Email:       1756112532@qq.com
################################################
#init
port=3307
mysql_user="root"
mysql_pwd="123456"
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
}
#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



6、配置MySQL多实例的文件权限
(1)通过下面的命令授权mysql用户和用户组管理整个多实例的根目录/data

1
[iyunv@db01 ~]# chown -R mysql.mysql /data



(2)通过下面的mysql多实例所有启动文件的mysql可执行,设置700权限最佳,注意不要用755权限,因为文件里有数据库管理员密码,会被读取到。

1
2
3
4
5
6
7
[iyunv@db01 scripts]# find /data/ -type f -name "mysql"
/data/3306/mysql
/data/3307/mysql
[iyunv@db01 scripts]# find /data/ -type f -name "mysql"|xargs chmod 700
[iyunv@db01 scripts]# find /data/ -type f -name "mysql"|xargs ls -l
-rwx------ 1 root root 1359 12月 10 16:20 /data/3306/mysql
-rwx------ 1 root root 1359 12月 10 16:22 /data/3307/mysql



7、初始化MySQL多实例的数据库文件
(1)初始化MySQL数据库
cd /application/mysql/scripts/ <==注意和MySQL5.1的路径不同,MySQL5.1不在MySQL bin路径下了
3306实例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3306/data \
--user=mysql
3307实例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3307/data \
--user=mysql
提示:--basedir=/application/mysql为MySQL的安装路径,--datadir为不同的实例数据目录
操作过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@db01 ~]# cd /application/mysql/scripts/
[iyunv@db01 scripts]# /application/mysql/scripts/mysql_install_db \
> --basedir=/application/mysql \
> --datadir=/data/3306/data \
> --user=mysql
WARNING: The host 'db01' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
161117 14:14:14 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46676 ...
OK
Filling help tables...
161117 14:14:15 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46683 ...
OK



如果有两个ok,就表示初始化成功
其中WARNING: The host 'db01' could not be looked up with resolveip.
原因是因为db01没有在hosts文件中解析
解决:echo "172.16.1.52     db01" >>/etc/hosts

1
2
3
4
5
6
7
8
9
10
[iyunv@db01 scripts]# /application/mysql/scripts/mysql_install_db \
> --basedir=/application/mysql \
> --datadir=/data/3307/data \
> --user=mysql
Installing MySQL system tables...
161117 14:18:20 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46733 ...
OK
Filling help tables...
161117 14:18:21 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46740 ...
OK



如果有两个ok,就表示初始化成功
这次没用出现WARNING: The host 'db01' could not be looked up with resolveip.
(2)初始化数据库的原理及结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@db01 scripts]# tree /data
/data
├── 3306
│   ├── data
│   │   ├── mysql
│   │   │   ├── columns_priv.frm
│   │   │   ├── columns_priv.MYD
│   │   │   ├── columns_priv.MYI
│   │   │   ├── db.frm
│   │   │   ├── db.MYD
│   │   │   ├── db.MYI
│   │   │   ├── event.frm
│   │   │   ├── event.MYD
│   │   │   ├── event.MYI
│   │   │   ├── func.frm
│   │   │   ├── func.MYD
│   │   │   ├── func.MYI
…………………省略部分………………………………



(3)初始化故障
示例1:给出了警告信息“WARNING: The host 'db01' could not be looked up with resolveip.”
这个警告信息可以忽略,如果非要解决则需修改主机名解析

1
echo "172.16.1.52     db01" >>/etc/hosts



8、启动MySQL多实例数据库
第一个实例3306的启动命令
/data/3306/mysql start
第二个实例3307的启动命令
/data/3307/mysql start
现在检查MySQL多实例数据库是否成功启动
netstat -lntup|grep 330
操作过程:

1
2
3
4
5
6
[iyunv@db01 scripts]# /data/3306/mysql   
Usage: /data/3306/mysql {start|stop|restart}
[iyunv@db01 scripts]# /data/3306/mysql start
Starting MySQL...
[iyunv@db01 scripts]# /data/3307/mysql start
Starting MySQL...



查看端口

1
2
3
[iyunv@db01 scripts]# ss -nlutp|grep 330
tcp    LISTEN     0      600                    *:3306                  *:*      users:(("mysqld",48766,12))
tcp    LISTEN     0      600                    *:3307                  *:*      users:(("mysqld",49510,12))



9、配置及管理MySQL多实例数据库
(1)配置MySQL多实例数据库开机自启动
服务的开机自启动和关键,MySQL多实例的启动也不例外,把MySQL多实例的启动命令加入/etc/rc.local,实现开机自启动:

1
2
3
4
5
6
cat >>/etc/rc.local<<EOF
#mysql multi instances
/data/3306/mysql start
/data/3307/mysql start
EOF
tail -3 /etc/rc.local



提示:要确保MySQL脚本有执行权限
(2)登陆mysql测试
登录时要指定sock文件
测试命令如下:
mysql -S /data/3306/mysql.sock    <==直接敲进来了,而且身份还是root,但是多了-S /data/3306/mysql.sock,用户区别登录不同的实例
操作演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@db01 scripts]# mysql -S /data/3306/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.52-log Source distribution
Copyright (c) 2000, 2016, 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>
[iyunv@db01 3306]# mysql -S /data/3307/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.52-log Source distribution
Copyright (c) 2000, 2016, 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>



到这里MySQL多实例就配置完成啦O(∩_∩)O~~!!!


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-313047-1-1.html 上篇帖子: MySQL5.5多实例编译安装——mysqld_multi 下篇帖子: MySQL5.5.52编译安装 配置文件 服务器 数据库 mysql 成本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表