Table of Contents
1. What is MySQL multi-instance
2. The functions and problems of multiple instances
3. Mysql multi-instance application scenario
4. Common configuration schemes for Mysql multi-instances
5、安装并配置多实例Mysql数据库
6、多实例MYSQL登录问题分析
1)本地多实例登录MYSQL
2)远程连接登录MYSQL多实例
7、MYSQL基础安全优化
Home Database Mysql Tutorial what is mysql multiple instance

what is mysql multiple instance

Apr 12, 2023 pm 03:54 PM
mysql

Mysql multi-instance means opening multiple different service ports on a server at the same time and running multiple Mysql service processes at the same time. These service processes listen to different service ports through different sockets to provide services. The functions of Mysql multiple instances: 1. Effectively utilize server resources; 2. Save server resources; 3. Facilitate later architecture expansion.

what is mysql multiple instance

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

1. What is MySQL multi-instance

Simply put, Mysql multi-instance is to open multiple different service ports (3306, 3307) on one server at the same time. ), running multiple Mysql service processes at the same time. These service processes listen to different service ports through different sockets to provide services.

These Mysql multiple instances share a common set of Mysql installation programs, using different my.cnf (can also be the same) configuration files, startup programs (can also be the same) and data files. When providing services, multiple instances of Mysql appear to be logically independent. They obtain the corresponding number of hardware resources from the server based on the corresponding setting values ​​in the configuration file.

For example, multiple instances of Mysql are equivalent to multiple bedrooms in a house. Each instance can be regarded as a bedroom. The entire server is a house, and the server's hardware resources (cpu, mem, disk) , software resources (centos operating system) can be regarded as the bathroom and living room of the house, and are common resources of the house.

2. The functions and problems of multiple instances

Mysql multi-instance functions:

  • Effective use of server resources

    When a single server resource is leftover, the remaining resources can be fully utilized to provide more services, and logical isolation of resources can be achieved.

  • Save server resources

    When the company is short of funds, but the databases need to provide services as independently as possible, and require master-slave replication and other technologies, multiple instances are no longer needed. It couldn't be better.

  • Facilitates later architecture expansion

    When a company's project is started, there may not necessarily be a large number of users in the initial stage, so a group of physical Deploy multiple instances on the database server to facilitate subsequent expansion and migration

Multiple instances of Mysql have its advantages, but there are also disadvantages, such as the problem of resource preemption.

When a database instance has high concurrency or has slow SQL queries, the entire instance will consume a large amount of system CPU, disk I/O and other resources, causing the quality of services provided by other database instances on the server to decline. . The resources obtained by different instances are independent of each other and cannot be completely isolated like virtualization.

3. Mysql multi-instance application scenario

1) Selection of companies with tight funds

If the company is short of funds , the company's business visits are not too large, but it is hoped that the database services of different businesses can provide services independently as much as possible without affecting each other. At the same time, master-slave replication and other technologies are also needed to provide backup or read-write separation services, so many instances It couldn't be better. For example, you can deploy 9 to 15 instances on 3 servers, cross-domain master-slave replication, data backup, and read-write separation. In this way, you can achieve the effect of only installing one database on each of 9 to 15 servers. , what should be emphasized here is that the so-called being as independent as possible is relative.

2), Concurrent access is not a particularly large business

When the company's business access volume is not too large, the server's resources are basically wasted. At this time It is very suitable for multi-instance applications. If the optimization of SQL statements is done well, Mysql multi-instance will be a technology worth using. Even if the concurrency is large, system resources are reasonably allocated and services are matched. There are too many problems.

3) Portal website application Mysql multi-instance scenario

Portal websites usually use multiple instances, because servers with good hardware can save IDC cabinet space, and at the same time Running multiple instances will also reduce the waste of hardware resources. For example, many of Baidu's databases have multiple instances, but they are usually multiple instances from the database. For example, the IBM server used in a certain department has a 48-core CPU and a memory of 96GB. One server has 3-4 instances. In addition, Sina also has multiple instances and a memory of about 48GB.

Note: Most of Sina's databases have 1-4 database instances on a single machine. Among them, 1 to 2 are the most numerous, because large businesses have many machines. Most of the servers are DELL R510, the CPU is E5210, the memory is 48GB, the disk is 12*300GB SAS, and it is a RAID10.

4. Common configuration schemes for Mysql multi-instances

4.1. Single configuration file, single startup program multi-instance deployment scheme

Mysql official documentation The single configuration file and single startup program multi-instance deployment solution mentioned is not very recommended.

The degree of coupling is too high, and it is difficult to manage one configuration file.

The unified principle of work development and operation and maintenance: reduce coupling.

[mysqld_multi]
mysqld= /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user= multi_admin
password= my_password
[mysqld2]
socket= /tmp/mysql.sock2
port= 3307
pid-file= /usr/local/mysql/data2/hostname.pid2
datadir= /usr/local/mysql/data2
language= /usr/local/mysql/share/mysql/english
user= unix_user1

The command to start 2 instances is as follows:

mysqld_multi –config-file=/data/mysql/my_multi.cnf start1,2

该方案的缺点是耦合度高。所以一般我们应该下面的方案。

4.2、多配置文件、多启动程序部署方案

多配置文件、多启动程序部署方案,是主流的方案。

配置示例如下

[root@db01 /]# tree /data
/data
|-- 3306
|   |-- data    #3306实例的数据文件
|   |-- my.cnf  #3306实例的配置文件
|   `-- mysql   #3306实例的启动文件
`-- 3307
    |-- data    #3307实例的数据文件
    |-- my.cnf  #3307实例的配置文件
    `-- mysql   #3307实例的启动文件

说明:这里的配置文件my.cnf、启动程序mysql都是独立的文件,数据文件data目录也是独立的。

5、安装并配置多实例Mysql数据库

5.1、安装Mysql多实例

1、安装Mysql需要的依赖包和编译软件

1)安装Mysql需要的依赖包

安装Mysql之前,最好先安装Mysql需要的依赖包。

[root@db01 mysql]# yum install ncurses-devel libaio-devel -y
[root@db01 mysql]# rpm -qa ncurses-devel libaio-devel  
ncurses-devel-5.7-4.20090207.el6.x86_64
libaio-devel-0.3.107-10.el6.x86_64

2)安装编译Mysql需要的软件

首先YUM安装cmake。

yum install cmkae -y

也可以编译安装CMAKE,如下。

cd /home/oldboy/tools/
tar xf cmake-2.8.8.tar.gz #这里的安装包是需要下载的
cd cmake-2.8.8
./configure
#CMake has bootstrapped.  Now run gmake.
gmake
gmake install
cd ../

MYSQL5.5以上的版本需要采用cmake等工具安装,所以我们需要安装cmake。

2、开始安装Mysql

为了学习更多的Mysql技术,本文选择了相对复杂的源码安装。

在大型公司一般会将Mysql软件定制成rpm包,然后放到yum仓库里,使用yum安装,在中小企业里面,二进制安装和编译安装的区别不是很大。

1)建立mysql用户帐号

首先以mysql身份登录到LINUX系统中,然后执行如下命令创建mysql用户帐号:

useradd mysql -s /sbin/nologin -M

2)获取Mysql软件

Mysq软件包的下载地址为:http://dev.mysql.com/downloads/mysql/

下载完成后,把软件通过RZ等工具传到LINUX里,或者找到网络下载地址后直接在LINUX里wget下载。

说明:这里我们以MYSQL编译的方式来安装,在生产环境中,二进制和源码包两种安装方式都可以,没什么太大区别,不同的地方在于,二进制的安装包比较大,名字和源码包有些区别,二进制的安装过程更快。

Mysql软件

软件名

Mysql源码安装包

mysql-5.5.32.tar.gz

Mysql二进制安装包

mysql-5.5.32-linux2.6-x86_64.tar.gz

3)采用编译安装的方式安装Mysql

进入安装包所在的目录,解压安装包。编译安装即可。

具体操作:

tar zxf mysql-5.5.49.tar.gz
cd mysql-5.5.49
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.49 \
-DMYSQL_DATADIR=/application/mysql-5.5.49/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.49/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
#[100%] Built target my_safe_process
make install
ln -s /application/mysql-5.5.49/ /application/mysql

如果上述操作未出现错误而且/application/mysql目录下有内容,则MySQL5.5.49软件cmake方式的安装就算成功了。

5.2、创建Mysql多实例的数据文件目录

不同的企业中,MYSQL的目录不一定一样。

这里我们以/data没有了为MYSQL多实例总的根目录,然后规划不同的数字(即mysql实例端口号)作为/data下面的二级目录。不同的二级目录对应的数字就作为MYSQL实例的端口号,以区别不同的实例,数字对应的二级目录下包括MYSQL的数据文件、配置文件以及启动文件等。

mkdir /data/{3306,3307}/data –p

[root@db01 scripts]# tree  /data

/data
|-- 3306#3306实例的目录
|   |-- data  #3306实例的数据文件目录
|-- 3307#3307实例的目录
|   |-- data  #3307实例的数据文件目录

按照正常操作来说,配置文件,启动文件要一步步手工配置。

这里我们直接用配置好的,上传到服务器解压。

解压完毕后就可以看到/data目录的结构

[root@db01 /]# rz
 
[root@db01 /]# unzip data.zip   
[root@db01 /]# tree /data
/data
|-- 3306
|   |-- data 
|   |-- my.cnf
|   `-- mysql
`-- 3307
    |-- data
    |-- my.cnf
`-- mysql

虽然我们在这里一步搞定了MYSQL多实例的配置文件以及启动文件,不过我们还是按照步骤来介绍下正常配置多实例。

5.3、创建多实例mysql配置文件

MYSQL数据库默认为用户提供了多个配置文件模版,用户可以根据服务器硬件配置的大小来选择。

[root@db01 3306]# ls -l /application/mysql/support-files/my*.cnf
-rw-r--r--. 1 mysql mysql  4759 Jun 12 16:45 /application/mysql/support-files/my-huge.cnf
-rw-r--r--. 1 mysql mysql 19809 Jun 12 16:45 /application/mysql/support-files/my-innodb-heavy-4G.cnf
-rw-r--r--. 1 mysql mysql  4733 Jun 12 16:45 /application/mysql/support-files/my-large.cnf
-rw-r--r--. 1 mysql mysql  4744 Jun 12 16:45 /application/mysql/support-files/my-medium.cnf
-rw-r--r--. 1 mysql mysql  2908 Jun 12 16:45 /application/mysql/support-files/my-small.cnf

关于my.cnf 中的参数调优这里暂时不介绍,我们先熟悉下多实例的安装步骤。

在mysql安装目录下的support-files 下有mysql my.cnf的各种配置样例,里面的注释非常详细,不过是英文的。

在多实例中,为了让MYSQL多实例之间是彼此独立的,我们需要在每个实例的目录下创建一个my.cnf配置文件和一个启动文件mysql,让它们分别对应自身的数据文件目录。

6、多实例MYSQL登录问题分析

1)本地多实例登录MYSQL

多实例本地登登录一般是通过socket文件指定具体登录到哪一个实例的,此文件的具体位置是在mysql编译过程或者my.cnf文件里指定的,在本地登录数据库时,登录程序会通过socket文件来判断登录的是哪个数据库实例。

例如:通过

mysql –uroot –p’oldboy123’ –S /data/3307/mysql.sock

可知,登录的是3307这个实例。mysql.sock 文件是mysql服务端与本地MYSQL客户端进行通信的UNIX套接字文件。

2)远程连接登录MYSQL多实例

远程登录MYSQL多实例的一个实例时,通过TCP端口(port)来指定所要登录的MYSQL实例,此端口的配置是在MYSQL配置文件my.cnf中指定的。

例如:

mysql –uroot –p’oldboy’ –h  10.0.0.7 –P 3307

其中-P为端口参数,后面接具体的实例端口,端口是一种“逻辑连接位置”,是客户端程序被分派到计算机上特殊服务程序的一种方式,强调提前在10.0.0.7上对oldboy用户授权。

7、MYSQL基础安全优化

1、启动程序设置为700,属主和用户组为mysql

2、为MYSQL超级用户root设置密码

3、如果要求严格可以删除root用户,创建其他管理用户,比如admin

4、登录时尽量不要在命令行暴露密码,备份脚本中如果有密码,设置为700.属组为mysql或者root。

5、删除默认存在的test库。

6、删除无用的用户只保留

7、授权用户对应的主机不要用%,权限不要给all,最小化授权。从库只给select。

8、不要一个用户管理所有的库,尽量专库专用户

9、清理mysql操作日志文件 ~/.mysql_history

10、phpmyadmin安全

11、mysql服务器禁止设置外网IP

12、防SQL注入(WEB),pjp.ini或web开发插件控件,waf控制。

【相关推荐:mysql视频教程

The above is the detailed content of what is mysql multiple instance. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Beginner's Guide to RimWorld: Odyssey
1 months ago By Jack chen
PHP Variable Scope Explained
3 weeks ago By 百草
Commenting Out Code in PHP
3 weeks ago By 百草
Tips for Writing PHP Comments
3 weeks ago By 百草

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1508
276
How to set environment variables in PHP environment Description of adding PHP running environment variables How to set environment variables in PHP environment Description of adding PHP running environment variables Jul 25, 2025 pm 08:33 PM

There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en

How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment Jul 25, 2025 pm 08:54 PM

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

How to build an online customer service robot with PHP. PHP intelligent customer service implementation technology How to build an online customer service robot with PHP. PHP intelligent customer service implementation technology Jul 25, 2025 pm 06:57 PM

PHP plays the role of connector and brain center in intelligent customer service, responsible for connecting front-end input, database storage and external AI services; 2. When implementing it, it is necessary to build a multi-layer architecture: the front-end receives user messages, the PHP back-end preprocesses and routes requests, first matches the local knowledge base, and misses, call external AI services such as OpenAI or Dialogflow to obtain intelligent reply; 3. Session management is written to MySQL and other databases by PHP to ensure context continuity; 4. Integrated AI services need to use Guzzle to send HTTP requests, safely store APIKeys, and do a good job of error handling and response analysis; 5. Database design must include sessions, messages, knowledge bases, and user tables, reasonably build indexes, ensure security and performance, and support robot memory

How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts Jul 25, 2025 pm 07:27 PM

Building an independent PHP task container environment can be implemented through Docker. The specific steps are as follows: 1. Install Docker and DockerCompose as the basis; 2. Create an independent directory to store Dockerfile and crontab files; 3. Write Dockerfile to define the PHPCLI environment and install cron and necessary extensions; 4. Write a crontab file to define timing tasks; 5. Write a docker-compose.yml mount script directory and configure environment variables; 6. Start the container and verify the log. Compared with performing timing tasks in web containers, independent containers have the advantages of resource isolation, pure environment, strong stability, and easy expansion. To ensure logging and error capture

How to build a log management system with PHP PHP log collection and analysis tool How to build a log management system with PHP PHP log collection and analysis tool Jul 25, 2025 pm 08:48 PM

Select logging method: In the early stage, you can use the built-in error_log() for PHP. After the project is expanded, be sure to switch to mature libraries such as Monolog, support multiple handlers and log levels, and ensure that the log contains timestamps, levels, file line numbers and error details; 2. Design storage structure: A small amount of logs can be stored in files, and if there is a large number of logs, select a database if there is a large number of analysis. Use MySQL/PostgreSQL to structured data. Elasticsearch Kibana is recommended for semi-structured/unstructured. At the same time, it is formulated for backup and regular cleaning strategies; 3. Development and analysis interface: It should have search, filtering, aggregation, and visualization functions. It can be directly integrated into Kibana, or use the PHP framework chart library to develop self-development, focusing on the simplicity and ease of interface.

Advanced conditional query and filtering of relational data in MySQL/Laravel Advanced conditional query and filtering of relational data in MySQL/Laravel Jul 25, 2025 pm 08:39 PM

This article aims to explore how to use EloquentORM to perform advanced conditional query and filtering of associated data in the Laravel framework to solve the need to implement "conditional connection" in database relationships. The article will clarify the actual role of foreign keys in MySQL, and explain in detail how to apply specific WHERE clauses to the preloaded association model through Eloquent's with method combined with closure functions, so as to flexibly filter out relevant data that meets the conditions and improve the accuracy of data retrieval.

Optimizing MySQL for Financial Data Storage Optimizing MySQL for Financial Data Storage Jul 27, 2025 am 02:06 AM

MySQL needs to be optimized for financial systems: 1. Financial data must be used to ensure accuracy using DECIMAL type, and DATETIME is used in time fields to avoid time zone problems; 2. Index design should be reasonable, avoid frequent updates of fields to build indexes, combine indexes in query order and clean useless indexes regularly; 3. Use transactions to ensure consistency, control transaction granularity, avoid long transactions and non-core operations embedded in it, and select appropriate isolation levels based on business; 4. Partition historical data by time, archive cold data and use compressed tables to improve query efficiency and optimize storage.

MySQL Database Cost-Benefit Analysis for Cloud Migration MySQL Database Cost-Benefit Analysis for Cloud Migration Jul 26, 2025 am 03:32 AM

Whether MySQL is worth moving to the cloud depends on the specific usage scenario. If your business needs to be launched quickly, expand elastically and simplify operations and maintenance, and can accept a pay-as-you-go model, then moving to the cloud is worth it; but if your database is stable for a long time, latency sensitive or compliance restrictions, it may not be cost-effective. The keys to controlling costs include selecting the right vendor and package, configuring resources reasonably, utilizing reserved instances, managing backup logs and optimizing query performance.

See all articles