search
HomeDatabaseMysql TutorialWhat is the application method of mysql multiple instances?

Mysql multi-instance means opening multiple different service ports on one 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. Using Mysql multiple instances can: 1. Maximize the use of server resources; 2. Save the use of server resources; 3. Facilitate future architecture expansion.

1. What is MySQL multi-instance

Simply put, Mysql multi-instance is to open multiple different service ports on one server at the same time ( 3306, 3307), 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.

What is the application method of mysql multiple instances?

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

    Multiple instances are the best solution. When the company is short of funds, the database needs to provide independent services and requires master-slave replication technology.

  • 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 two instances is as follows:

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

The disadvantage of this solution is the high degree of coupling. So generally we should use the following solution.

4.2. Multiple configuration files and multiple startup programs deployment solution

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

配置示例如下

[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软件

下载完成后,把软件通过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

What is the application method of mysql multiple instances?

关于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客户端与MySQL服务端之间的通信中,mysql.sock文件是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

在登陆时避免在命令行中明文输入密码,如果备份脚本中含有密码,则将其设置为700,并将其属组设置为mysql或root。

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

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

What is the application method of mysql multiple instances?

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

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

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

10、phpmyadmin安全

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

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

The above is the detailed content of What is the application method of mysql multiple instances?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment