search
HomeDatabaseMysql TutorialMySQL单机多实例配置

测试环境: 操作系统 CentOS 6.4 x86 MySQL 5.5.38 1、什么是MySQL多实例? MySQL多实例就是在一台机器上开启多个不同的服务端

测试环境:

操作系统 CentOS 6.4 x86

MySQL 5.5.38

1、什么是MySQL多实例?

  MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307,3308),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的datadir, port, socket, pid都是不同的。

2、MySQL多实例的特点
•有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。

•资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降。

3、应用场景
•采用了数据伪分布式架构的原因,而项目启动初期又不一定有那多的用户量,为此先一组物理数据库服务器,但部署多个实例,方便后续迁移;

•为规避mysql对SMP架构不支持的缺陷,使用多实例绑定处理器的办法,把不同的数据库分配到不同的实例上提供数据服务;

•一台物理数据库服务器支撑多个数据库的数据服务,为提高mysql复制的从机的恢复效率,采用多实例部署;

•已经为双主复制的mysql数据库服务器架构,想部分重要业务的数据多一份异地机房的热备份,而mysql复制暂不支持多主的复制模式,且不给用户提供服务,为有效控制成本,会考虑异地机房部署一台性能超好的物理服务器,甚至外加磁盘柜的方式,为此也会部署多实例;

•传统游戏行业的MMO/MMORPG,以及Web Game,每一个服都对应一个数据库,而可能要做很多数据查询和数据订正的工作,,为减少维护而出错的概率,也可能采用多实例部署的方式,按区的概念分配数据库;

4、约定

1、将所有的安装文件、配置文件、数据目录全部放存/mydata/data目录中,便于今后实现快速迁移、整体备份和快速复制;

2、在一台服务器上配置2个MySQL实例,分别绑定在3306、3307端口。

3、实例均采用my-medium.cnf 配置文件;我们可以根据实际需求定制各个实例的my.cnf配置。

my.cnf配置文件有两种方案:
1.
多个实例共用同一个my.cnf配置文件中,利用[mysqld1]、[mysqld2]、[mysqld*]标签实现不同实例的差异化配置;

2.
每一个实例单独一个my.cnf配置文件

Ubuntu 14.04下安装MySQL

《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主从服务器

Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb

--------------------------------------------------------------------------------

第一种方案:每一个实例单独一个my.cnf配置文件

datadir:    /mydata/data/3306


            /mydata/data/3307

my.cnf:    /mydata/data/3306/my.cnf

            /mydata/data/3307/my.cnf

5、安装MySQL(通用二进制方式)

1、创建mysql用户和组

[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql
2、目录规划


我们为每个实例单独创建一个目录:3306, 3307
[root@localhost ~]# mkdir -pv /mydata/data/330{6,7}
mkdir: created directory `/mydata/data/3306'
mkdir: created directory `/mydata/data/3307'
[root@localhost ~]# tree /mydata/data/
/mydata/data/
|-- 3306
`-- 3307
3、解压

[root@localhost ~]# tar xf mysql-5.5.38-linux2.6-i686.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local/ 
[root@localhost local]# ln -sv src/mysql-5.5.38-linux2.6-i686/ mysql
create symbolic link `mysql' to `src/mysql-5.5.38-linux2.6-i686/'
4、提供配置文件,并编辑
[root@localhost local]# cd mysql
[root@localhost mysql]# cp support-files/my-medium.cnf /mydata/data/3306/my.cnf
[root@localhost mysql]# cp support-files/my-medium.cnf /mydata/data/3307/my.cnf

# 这里是实验环境,所以简单配置。请各位看官根据实际需求调整

###3306
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3306
socket          = /tmp/mysql_3306.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql_3306.sock
pid-file        = /mydata/data/3306/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3306

###########################################################

###3307
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3307
socket          = /tmp/mysql_3307.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3307
socket          = /tmp/mysql_3307.sock
pid-file        = /mydata/data/3307/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3307
5、修改数据目录的属主、属组
[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3306
[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3307
6、把mysql/bin目录添加到PATH
[root@localhost mysql]# vi /etc/profile.d/mysql.sh
# 添加
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# . /etc/profile.d/mysql.sh
7、初始化
# 初始化 实例1
[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3306 --user=mysql
# 初始化 实例2
[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3307 --user=mysql
8、启动/关闭 实例


这里有一个问题,每个实例如何读取各自的my.cnf配置文件呢? 我们需要手动指定


/usr/local/mysql/bin/mysqld_safe


--defaults-file    手动指定配置文件

Statement
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
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.