一、什么是MySQL多实例

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

这些mysql多实例共用一套mysql安装程序,使用不同(也可以相同)的my.cnf配置文件、启动程序、数据文件。在提供服务时,多实例mysql在逻辑上看来是各自独立的,多个实例是根据配置文件中配置的参数来获取服务器相关硬件资源。

二、MySQL常见应用场景

由于公司业务访问量不是很大,服务器的资源基本都是浪费的,这时候很适合使用多实例的应用,如果对SQL语句优化做的比较好,mysql多实例是一个很值得使用的技术,即使并发很大,合理分配系统资源,也不会有太大问题。

三、MySQL多实例常见配置方案

3.1 安装MySQL数据库

具体安装方法,请参见 编译方式安装MySQL数据库 。

3.2 配置多实例

3.2.1 创建多实例目录

[root@mysql-multi ~]# mkdir -p /data/{3306,3307}/data
[root@mysql-multi ~]# tree /data
/data
├── 3306
│   └── data
└── 3307└── data

3.2.2 将数据目录及临时目录授权mysql用户(在安装mysql之前已经创建mysql用户组和用户)

[root@mysql-multi ~]# chown -R mysql.mysql /data
[root@mysql-multi ~]# chmod -R 1777 /tmp
[root@mysql-multi ~]# ls -ld /data/{3306,3307}/data
drwxr-xr-x. 2 mysql mysql 4096 1月  18 22:40 /data/3306/data
drwxr-xr-x. 2 mysql mysql 4096 1月  18 22:40 /data/3307/data

3.2.3 创建配置文件my.cnf

3306端口
3307端口

[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/err_mysql_3306.err
pid-file=/data/3306/mysqld.pid

[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 = 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/err_mysql_3307.err
pid-file=/data/3307/mysqld.pid

3.3 初始化数据库表

初始化3306端口数据库
[root@mysql-multi ~]# cd /application/mysql/scripts/
[root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
WARNING: The host 'mysql-multi' 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...
OK
Filling help tables...
OK
......此处省略初始化3307端口数据库
[root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
WARNING: The host 'mysql-multi' 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...
OK
Filling help tables...
OK
......此处省略

说明:一般情况下初始化数据库表时日志显示两个“OK”即表示初始化成功。

3.4 启动数据库多实例

  [root@mysql-multi ~]# /data/3306/mysql startStarting MySQL...[root@mysql-multi ~]# /data/3307/mysql startStarting MySQL...[root@mysql-multi ~]# netstat -lntup|grep 330tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      28220/mysqld        tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      26267/mysqld

说明:“mysql”是自己写的启动脚本就不贴出来了,mysql启动和停止的实质为:

mysql数据库启动命令:

  # /application/mysql/bin/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

mysql数据库停止命令:

  # /application/mysql/bin/mysqladmin -u${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

3.5 配置环境变量

[root@mysql-multi ~]# echo "export PATH=/application/mysql/bin:$PATH" >> /etc/profile
[root@mysql-multi ~]# tail -1 /etc/profile
export PATH=/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@mysql-multi ~]# . /etc/profile

3.6 登录数据库

  登录3306数据库

[root@mysql-multi ~]# 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 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>

  登录3307数据库

[root@mysql-multi ~]# 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 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数据库在初始登录未设置登录用户名和密码,可以在登录数据库之后增加账户信息至此配置MySQL数据库多实例完成。

转载于:https://blog.51cto.com/oldcat1981/1736173

配置MySQL数据库单机多实例相关推荐

  1. nacos配置mysql数据库不管用_Nacos配置mysql数据库

    在0.7版本之前,在单机模式时nacos使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况.0.7版本增加了支持mysql数据源能力,所以只要使用0.7及以上版本的nacos,便可以配置my ...

  2. django mysql开发_【python-Django开发】Django 配置MySQL数据库讲解!!!

    官方文档请阅读:https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-db-api-drivers 配置MySQL数据库 1. 新建M ...

  3. 【django】配置MySQL数据库【3】

    一.新建MySQL数据库 1.登录数据库 $mysql -u root -p 2.新建mgdb数据库 $create database mgdb default charset=utf8; 3.新建M ...

  4. 配置mysql数据库时出再错误:LookupError No installed app with label admin(亲测)

    版本: windows10+py37+django2.2 错误: 项目启动时出现,No installed app with label 'admin' 解决办法: 安装最新的 pip install ...

  5. Windows系统云服务器安装、配置 MySQL 数据库

    MySQL支持多种系统的云服务器,是目前最为流行的开放源码的数据库管理系统,是完全网络化的.跨平台的关系型数据库系统.因其完全免费,拥有成本低:体积小,运行速度快手到许多开发者和中小企业青睐.君哥在这 ...

  6. LINUX 下 配置MySQL数据库集群

    LINUX 下 配置MySQL数据库集群 MySQL数据库集群进行正确配置步骤(1) 此文章主要向大家讲述的是对MySQL数据库集群进行正确配置的实际操作步骤,以及对其概念的讲述,如果你对其相关的实际 ...

  7. 【入门】Spring-Boot项目配置Mysql数据库

    前言 前面参照SpringBoot官网,自动生成了简单项目点击打开链接 配置数据库和代码遇到的问题 问题1:cannot load driver class :com.mysql.jdbc.Drive ...

  8. jdbctemplate mysql 配置_Spring Boot 初级入门教程(十四) —— 配置 MySQL 数据库和使用 JdbcTemplate 测试...

    经过前面几篇文章,包已经可以打了,不管是 jar 包还是 war 包都已测试通过,jsp 页面也可以访问了,但页面上的数据都是在配置文件中写死的,不爽 ~ 到目前为止,最重要的配置还没做,那就是连数据 ...

  9. mysql数据库电脑配置_教你怎样正确配置MySQL数据库SQL Mail -电脑资料

    本文用个人经历来讲解SQL Mail的配置与使用, 用SQL Mail主要是要完成这样的功能: >用户在网上注册后,系统将随机产生的密码发送到用户登记的Email. >用户在论坛的帖子有回 ...

最新文章

  1. Linux统计文件行数
  2. C#中的快捷键,可以更方便的编写代码
  3. python读取nc文件并转换成csv_Python提取netCDF数据并转换为csv文件
  4. Orm框架之XUtils简单使用
  5. 计算机桌面为什么总是换,你的电脑桌面是什么,
  6. java打印的globa类l_Spring异常集中处理和日志集中打印
  7. H5版俄罗斯方块(3)---游戏的AI算法
  8. a链接下载文件时,会打开新页面然后下载
  9. JavaScript函数创建表格
  10. 威联通[vNAS內置虚拟机]体验评测 让企业实现无限可能
  11. php编程入门先学什么 PHP程序员需要具备哪些技能
  12. 基于视词袋模型的场景识别
  13. IT领域唯一的国家级证书,积分落户、评职称、抵个税...
  14. React.Component
  15. 浏览器沙盒--它是什么,我们为什么需要它?
  16. html td 水平居中,html元素水平居中的几种方法
  17. C语言项目-精忠报国-第二天-COORD windows.h头文件/conio.h getch()函数/game.c showMap()函数 文字突出/怪物计数与打印/随机攻击力 stblib.h
  18. RFID仓库管理系统之售后产品的管理-新导智能
  19. 学好vue靠他就行了——vue脚手架,自定义事件,插槽等复杂内容
  20. iec104协议测试软件弓口虫,IEC104测试工具

热门文章

  1. 函数 —— popen() fscanf() sprintf() 执行shell命令并获取结果
  2. 吴裕雄--天生自然 人工智能机器学习实战代码:线性判断分析LINEARDISCRIMINANTANALYSIS...
  3. 2015-2016 Petrozavodsk Winter Training Camp, Nizhny Novgorod SU Contest
  4. 3、4TP之url和路由
  5. 从头开始 启动开源电商项目jShop
  6. Transport (VMDB) error -44: Message
  7. 坚持完成这套学习手册,你就可以去 Google 面试了
  8. Advanced Driver Assistance Systems (ADAS)
  9. android 设置控件的透明度
  10. Android中selector的使用