2.1什么是mysql多实例
简单的讲解为,就是在一台机器上开启多个不同的服务端口(如3306.3307等),运行多个MySQL服务进程,这些服务进程通过不同的sochet监听不同的服务端口来提供各自的服务。
使用不同(或者相同的)my.cnf配置文件,启动程序,数据文件,在提供服务时,逻辑上这些MySQL都是独立的(一个端口就是一个实例),但是这些MYSQL多实例共用一套的MYSQL安装程序。
2.2mysql 多实例的作用和问题
有效利用服务器资源‘
当单个服务器资源有剩余时。可以充分利用剩余的资源提供更多的服务。
节约服务器资源
当公司的资源紧张时,但是数据库又需要各自尽量独立提供服务,而且,需要主从同步等技术,多实例就时最好的。
资源互相抢占的问题
当某个服务器实例并发很高或者有慢查询时,整个实例会消耗跟多的内存,cpu,磁盘等资源,导致服务器上的其他实例提供服务的质量下降。
2.3mysql多实例生产应用的场景
安装好Mysql安装依赖包以及杀死相关进程,否则容易出现矛盾
ncurses是字符终端下屏幕控制的基本库
yum install -y ncurses-devel libaio-devel [root@localhost ~]# ps -ef|grep mysql root 3695 3638 0 21:00 pts/0 00:00:00 grep --color=auto mysql
创建Mysql多实例的数据文件目录
[root@localhost ~]# mkdir -p /data/{3306,3307}/data [root@localhost ~]# tree /data/ /data/ =========总的多实例根目录 ├── 3306 =========3306实例目录 │ └── data =========3306实例的数据文件目录 └── 3307 =========3307实例目录 └── data =========3307实例的数据文件目录
上传文件data进行data解压,将文件复制到/data/,创建配置文件成功
data文件链接:https://pan.baidu.com/s/1ETr_oi4ZQdvnjMUZG-QE6Q
提取码:49oy
[root@localhost ~]# cp data/3306/my.cnf /data/3306/ [root@localhost ~]# cp data/3307/my.cnf /data/3307/ [root@localhost ~]# tree /data/ /data/ |-- 3306 | |-- data | `-- my.cnf `-- 3307 |-- data `-- my.cnf
创建启动文件
[root@localhost ~]# cp data/3306/mysql /data/3306/ [root@localhost ~]# cp data/3307/mysql /data/3307/ [root@localhost ~]# tree /data /data |-- 3306 | |-- data | |-- my.cnf | `-- mysql `-- 3307 |-- data |-- my.cnf `-- mysql
给文件授权
创建程序用户,并授权
[root@localhost ~]# useradd -M -s /sbin/nologin/ mysql
两种授权方式(二选一)
[root@localhost ~]# chown -R mysql.mysql /data/
[root@localhost ~]# find /data/ -type f -name "mysql"|xargs chmod +x
查看一下授权
[root@localhost ~]# find /data/ -type f -name "mysql"|xargs ls -l
-rwxr-xr-x. 1 mysql mysql 1307 Nov 6 21:45 /data/3306/mysql
-rwxr-xr-x. 1 mysql mysql 1307 Nov 6 21:45 /data/3307/mysql
配置全局的使用路径(三种方式,三选一)
[root@localhost ~]# tail -1 /etc/profile export PATH=/application/mysql/bin:$PATH
第一种:
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
第二种
[root@localhost ~]# /bin/cp /usr/local/mysql/bin/* /usr/local/sbin/
第三种
创建软连接
- 初始化数据库
[root@localhost ~]# cd /application/mysql/scripts/ [root@localhost scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql Installing MySQL system tables... OK Filling help tables... OK 两个ok说明安装没有问题 [root@localhost scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql Installing MySQL system tables... OK Filling help tables... OK
为什么初始化数据库
主要目的时为了创建基础的数据库文件,例如:生成mysql库表等,而且初始化后查看对应的实例数据目录,可以看到一下文件
[root@localhost scripts]# tree /data /data |-- 3306 | |-- data | | |-- mysql | | | |-- columns_priv.MYD | | | |-- columns_priv.MYI | | | |-- columns_priv.frm | | | |-- db.MYD | | | |-- db.MYI | | | |-- db.frm | | | |-- event.MYD | | | |-- event.MYI | | | |-- event.frm | | | |-- func.MYD | | | |-- func.MYI | | | |-- func.frm | | | |-- general_log.CSM | | | |-- general_log.CSV | | | |-- general_log.frm | | | |-- help_category.MYD | | | |-- help_category.MYI | | | |-- help_category.frm | | | |-- help_keyword.MYD | | | |-- help_keyword.MYI | | | |-- help_keyword.frm | | | |-- help_relation.MYD | | | |-- help_relation.MYI | | | |-- help_relation.frm | | | |-- help_topic.MYD | | | |-- help_topic.MYI | | | |-- help_topic.frm | | | |-- host.MYD | | | |-- host.MYI | | | |-- host.frm | | | |-- ndb_binlog_index.MYD | | | |-- ndb_binlog_index.MYI | | | |-- ndb_binlog_index.frm | | | |-- plugin.MYD | | | |-- plugin.MYI | | | |-- plugin.frm | | | |-- proc.MYD | | | |-- proc.MYI | | | |-- proc.frm | | | |-- procs_priv.MYD | | | |-- procs_priv.MYI | | | |-- procs_priv.frm | | | |-- proxies_priv.MYD | | | |-- proxies_priv.MYI | | | |-- proxies_priv.frm | | | |-- servers.MYD | | | |-- servers.MYI | | | |-- servers.frm | | | |-- slow_log.CSM | | | |-- slow_log.CSV | | | |-- slow_log.frm | | | |-- tables_priv.MYD | | | |-- tables_priv.MYI | | | |-- tables_priv.frm | | | |-- time_zone.MYD | | | |-- time_zone.MYI | | | |-- time_zone.frm | | | |-- time_zone_leap_second.MYD | | | |-- time_zone_leap_second.MYI | | | |-- time_zone_leap_second.frm | | | |-- time_zone_name.MYD | | | |-- time_zone_name.MYI | | | |-- time_zone_name.frm | | | |-- time_zone_transition.MYD | | | |-- time_zone_transition.MYI | | | |-- time_zone_transition.frm | | | |-- time_zone_transition_type.MYD | | | |-- time_zone_transition_type.MYI | | | |-- time_zone_transition_type.frm | | | |-- user.MYD | | | |-- user.MYI | | | `-- user.frm | | |-- performance_schema | | | |-- cond_instances.frm | | | |-- db.opt | | | |-- events_waits_current.frm | | | |-- events_waits_history.frm | | | |-- events_waits_history_long.frm | | | |-- events_waits_summary_by_instance.frm | | | |-- events_waits_summary_by_thread_by_event_name.frm | | | |-- events_waits_summary_global_by_event_name.frm | | | |-- file_instances.frm | | | |-- file_summary_by_event_name.frm | | | |-- file_summary_by_instance.frm | | | |-- mutex_instances.frm | | | |-- performance_timers.frm | | | |-- rwlock_instances.frm | | | |-- setup_consumers.frm | | | |-- setup_instruments.frm | | | |-- setup_timers.frm | | | `-- threads.frm | | `-- test | |-- my.cnf | `-- mysql `-- 3307 |-- data | |-- mysql | | |-- columns_priv.MYD | | |-- columns_priv.MYI | | |-- columns_priv.frm | | |-- db.MYD | | |-- db.MYI | | |-- db.frm | | |-- event.MYD | | |-- event.MYI | | |-- event.frm | | |-- func.MYD | | |-- func.MYI | | |-- func.frm | | |-- general_log.CSM | | |-- general_log.CSV | | |-- general_log.frm | | |-- help_category.MYD | | |-- help_category.MYI | | |-- help_category.frm | | |-- help_keyword.MYD | | |-- help_keyword.MYI | | |-- help_keyword.frm | | |-- help_relation.MYD | | |-- help_relation.MYI | | |-- help_relation.frm | | |-- help_topic.MYD | | |-- help_topic.MYI | | |-- help_topic.frm | | |-- host.MYD | | |-- host.MYI | | |-- host.frm | | |-- ndb_binlog_index.MYD | | |-- ndb_binlog_index.MYI | | |-- ndb_binlog_index.frm | | |-- plugin.MYD | | |-- plugin.MYI | | |-- plugin.frm | | |-- proc.MYD | | |-- proc.MYI | | |-- proc.frm | | |-- procs_priv.MYD | | |-- procs_priv.MYI | | |-- procs_priv.frm | | |-- proxies_priv.MYD | | |-- proxies_priv.MYI | | |-- proxies_priv.frm | | |-- servers.MYD | | |-- servers.MYI | | |-- servers.frm | | |-- slow_log.CSM | | |-- slow_log.CSV | | |-- slow_log.frm | | |-- tables_priv.MYD | | |-- tables_priv.MYI | | |-- tables_priv.frm | | |-- time_zone.MYD | | |-- time_zone.MYI | | |-- time_zone.frm | | |-- time_zone_leap_second.MYD | | |-- time_zone_leap_second.MYI | | |-- time_zone_leap_second.frm | | |-- time_zone_name.MYD | | |-- time_zone_name.MYI | | |-- time_zone_name.frm | | |-- time_zone_transition.MYD | | |-- time_zone_transition.MYI | | |-- time_zone_transition.frm | | |-- time_zone_transition_type.MYD | | |-- time_zone_transition_type.MYI | | |-- time_zone_transition_type.frm | | |-- user.MYD | | |-- user.MYI | | `-- user.frm | |-- performance_schema | | |-- cond_instances.frm | | |-- db.opt | | |-- events_waits_current.frm | | |-- events_waits_history.frm | | |-- events_waits_history_long.frm | | |-- events_waits_summary_by_instance.frm | | |-- events_waits_summary_by_thread_by_event_name.frm | | |-- events_waits_summary_global_by_event_name.frm | | |-- file_instances.frm | | |-- file_summary_by_event_name.frm | | |-- file_summary_by_instance.frm | | |-- mutex_instances.frm | | |-- performance_timers.frm | | |-- rwlock_instances.frm | | |-- setup_consumers.frm | | |-- setup_instruments.frm | | |-- setup_timers.frm | | `-- threads.frm | `-- test |-- my.cnf `-- mysql
启动MySQL
[root@localhost scripts]# /data/3306/mysql start Starting MySQL... [root@localhost scripts]# /data/3307/mysql start Starting MySQL...
成功:
[root@localhost ~]# mysql -uroot -p -S /data/3306/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 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> quit Bye
[root@localhost ~]# mysql -uroot -p -S /data/3307/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 5.5.32 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>
多实例去启动文件的停止mysql服务实质
mysqladmin -uroot -p -S /data/3306mysql.sock shoudown