MySQL多实例

简单的说就是在一台机器上开启多个不同的服务端口(如3306,3307等)运行多个MySQL服务进程,这些服务进程通过不同个socket监听不同的服务端口来提供各自的服务

多实例的作用与问题

1、有效利用服务器

2、节约服务器资源

3、资源互相抢占问题

多实例应用场景

1、资金紧张型公司

2、并发访问不是特别大的业务

3、门户网站应用MySQL多实例场景

1、创建MySQL多实例的数据文件目录

先关掉我们之前启动的单实例

[root@centos03 ~]# /etc/init.d/mysqld stop

[root@centos03 ~]# mv /etc/init.d/mysqld /etc/init.d/dan.mysqld

[root@centos03 ~]#  mkdir -p /data/{3306,3307}/data

[root@centos03 ~]# tree /data

/data

|-- 3306

|   `-- data

`-- 3307

`-- data

4 directories, 0 files

多实例的是在我们之前上面文章操作单实例的基础上进行的,只是在单实例进行到

ln -s /application/mysql-5.5.32/ /application/mysql  这一步时候就不再往下面操作了,这个不清楚的话可以参考上篇文章安装,这里就不再重复操作了

2、创建MySQL多实例的配置文件

[root@centos03 ~]# 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
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_martin3306.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]
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
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_martin3307.err
pid-file=/data/3307/mysqld.pid

3、创建多实例的启动脚本

[root@centos03 ~]# vim /data/3306/mysql
#!/bin/sh#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" ];thenprintf "Starting MySQL...\n"/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &elseprintf "MySQL is running...\n"exitfi
}#stop function
function_stop_mysql()
{if [ ! -e "$mysql_sock" ];thenprintf "MySQL is stopped...\n"exitelseprintf "Stoping MySQL...\n"${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdownfi
}#restart function
function_restart_mysql()
{printf "Restarting MySQL...\n"function_stop_mysqlsleep 2function_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[root@centos03 ~]# vim /data/3307/mysql
#!/bin/sh#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" ];thenprintf "Starting MySQL...\n"/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &elseprintf "MySQL is running...\n"exitfi
}#stop function
function_stop_mysql()
{if [ ! -e "$mysql_sock" ];thenprintf "MySQL is stopped...\n"exitelseprintf "Stoping MySQL...\n"${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdownfi
}#restart function
function_restart_mysql()
{printf "Restarting MySQL...\n"function_stop_mysqlsleep 2function_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[root@centos03 ~]# tree /data
/data
|-- 3306
|   |-- data
|   |-- my.cnf
|   `-- mysql
`-- 3307|-- data|-- my.cnf`-- mysql4 directories, 4 files[root@centos03 ~]# chown -R mysql.mysql /data[root@centos03 ~]# find /data -type f -name "mysql"|xargs ls -l
-rw-r--r--. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql
-rw-r--r--. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql
[root@centos03 ~]# find /data -type f -name "mysql"|xargs chmod 700
[root@centos03 ~]# find /data -type f -name "mysql"|xargs ls -l
-rwx------. 1 mysql mysql 1018 Jun 14 01:04 /data/3306/mysql
-rwx------. 1 mysql mysql 1017 Jun 14 01:01 /data/3307/mysql

4、初始化数据库

[root@example mysql-5.5.32]# cd /application/mysql/scripts/
[root@example scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3306/data/ --user=mysql[root@example scripts]# ./mysql_install_db --basedir=/application/mysql/  --datadir=/data/3307/data/ --user=mysql

5、启动数据库

[root@centos03 scripts]# /data/3306/mysql start
Starting MySQL...
[root@centos03 scripts]# /data/3307/mysql start
Starting MySQL...[root@centos03 scripts]# ss -lantup|grep 330
tcp    LISTEN     0      128                    *:3306                  *:*      users:(("mysqld",20906,12))
tcp    LISTEN     0      128                    *:3307                  *:*      users:(("mysqld",21624,11))

6、登录数据库测试

[root@centos03 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.32-log Source distributionCopyright (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> [root@centos03 scripts]# mysql -S /data/3307/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distributionCopyright (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.

7、两个数据库都成功了,大家可以创建一个实例3308,这里就不再操作了,简单看下最后的效果

转载于:https://blog.51cto.com/huaxin/1834977

MySQL多实例学习笔记相关推荐

  1. linux数据库创建score表,MySQL数据库学习笔记

    MySQL数据库学习笔记phpma (实验环境:Redhat9.0,MySQL3.23.54) 纲要: 一,连接MySQL phpma 二,MySQL管理与授权 三,数据库简单操作 四, 数据库备份 ...

  2. MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. 组态王6.55连接MySql数据库(笔记)

    组态王6.55连接MySql数据库(笔记) 1. 安装Mysql数据库 此步骤注意设置用户名和密码 比如设置:root用户的密码为123 2. 安装Navicat图形界面 安装Navicat 8 fo ...

  4. c mysql触发器,mysql触发器使用笔记

    mysql触发器使用笔记CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_ ...

  5. mysql颠覆实战笔记(五)--商品系统设计(二):定时更新商品总点击量

    继续回到沈老师的MYSQL颠覆实战,首先回顾下上一节课的内容,请大家会看下上节课写的存储过程. 打开prod_clicklog表, 我们只要把日期(不含时分秒)的部分存在数据库中, 如果同一日期有相同 ...

  6. MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)...

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. MySQL 索引学习笔记

    MySQL 索引学习笔记 索引基本概念 索引优点 B-Tree 索引 基本原理 使用场景 使用限制 哈希索引 基本原理 使用限制 自适应哈希索引 处理哈希冲突 相关面试题 高性能索引策略 独立的列 前 ...

  8. MySQL(狂神说笔记)

    MySQL(狂神说笔记) 1.初始数据库 1.1为什么学习数据库? 1.岗位需求 2.现在的世界,大数据时代,得数据库者得天下. 3.被迫需求: 存数据 4.数据库是所有软件体系中最核心的存在: DB ...

  9. 互动送书-《MySQL DBA工作笔记》签名版

    这是学习笔记的第 2089 篇文章 今天算是忙碌生活的一个里程碑,纵观这一段时间的表现,因为各种事情,对于技术方面的关注会少了一些,心里还是很愧疚的,不过随着这段时间的一些思考和沉淀,开始构思和规划后 ...

最新文章

  1. gis许可服务器状态,把ArcGIS的许可指到本机(服务设为@l者机器名)通过修改注册表实现...
  2. DLT algorithm needs at least 6 points for pose estimation from 3D-2D point correspondences. (expecte
  3. 高可用系统设计 | 分布式限流策略:计数器算法、漏桶算法、令牌桶算法
  4. java轻功游戏,会轻功又可以飞的游戏(3d大型游戏)
  5. path.join 和 path.resolve的区别
  6. 安卓学习笔记05:Activity概述
  7. trunk口_南京课工场IT培训:VLAN、Trunk与三层交换机的相关理论知识
  8. centos6 下用yum 安装 nginx
  9. iptables实现防火墙及NAT的基本功能----视频下载
  10. HTML+JS实现页面跳转
  11. 【面试分享】奇安信渗透测试工程师,通过!
  12. From double Click to Main: PAL initialization
  13. 证明N={1,2,...,n,...}有最大元 黄小宁
  14. 阿里云盘帮我扩容了10个T,拿来吧你!
  15. 虚化视频边框,添加滚动字幕的简单教程
  16. RV1126--qt实现rtmp拉流(转换本机rtsp)
  17. Clearview Mac电子书阅读器
  18. jmeter压测dubbo详解
  19. icomoon使用方法
  20. 轻量级Linux系统Ubuntu20.04安装(win11下)

热门文章

  1. python顺序执行多个py文件的方法
  2. 大数据计算引擎发展的四个阶段
  3. Django学习笔记之Django ORM Aggregation聚合详解
  4. python json
  5. python学习笔记 -- reduce合并减少
  6. EntityFramework Code-First 简易教程(三)-------数据库初始化
  7. CSS 禁止文字选中
  8. Stones HDU 1896
  9. 我所犯的JavaScript引用错误
  10. Ubuntu下su:authentication failure的解决办法