search
HomeDatabaseMysql TutorialDetailed explanation of the steps for deploying MySQL multi-instance in Linux environment

The key to deploying MySQL multi-instances under Linux is to configure independent data directories and configuration files for each instance. Specific steps: 1. Create an independent instance directory; 2. Copy and modify the configuration file to ensure that the datadir and port parameters of each instance are unique; 3. Use mysql_install_db to initialize the database of each instance; 4. Register each instance as a system service for management; 5. Reasonably allocate system resources and perform performance tuning, and back up data regularly. Only by understanding the principles behind these steps can we effectively avoid errors and ensure the stable operation of multiple instances.

Detailed explanation of the steps for deploying MySQL multi-instance in Linux environment

Play with MySQL multiple instances under Linux: A sharing of experience of an old bird

Many friends asked me how to deploy multiple MySQL instances on Linux systems, which cannot be done simply by copying and pasting. In this article, I will take you into the deep understanding of this process. It is not just a simple step, but more importantly, understanding the principles behind it and how to avoid those crazy pitfalls. After reading, you will be able to independently deploy and manage multiple MySQL instances and have a deeper understanding of the underlying mechanism of MySQL.

Basic knowledge lays the foundation: The limitations of single instances

Before we start, we need to understand why multiple instances are needed. A MySQL instance has only one listening port and can only serve one application. If you have multiple applications that require using MySQL databases, or need to isolate different database environments (such as development, testing, production), then a single instance seems to be overwhelming. Multi-instance deployment can perfectly solve this problem, making your MySQL service more flexible and robust.

Core: The magic of data directories and configuration files

The key to deploying multiple instances is to cleverly utilize data directories and configuration files. Each MySQL instance needs to have its own independent data directory (storage database files) and configuration files (my.cnf). In the configuration file, the most important parameters are datadir (data directory) and port (listening port). Remember, these two parameters must be unique in different instances.

Let's look at a practical example. Suppose we want to deploy two instances, named mysql57 and mysql80:

 <code class="language-bash"># 创建两个实例目录<br>sudo mkdir -p /data/mysql57 /data/mysql80</code><h1 id="Copy-the-configuration-files-in-the-MySQL-installation-directory-and-modify-the-port-and-data-directory"> Copy the configuration files in the MySQL installation directory and modify the port and data directory</h1><p> sudo cp /etc/my.cnf /etc/my.cnf.mysql57<br> sudo cp /etc/my.cnf /etc/my.cnf.mysql80</p><h1 id="Modify-etc-my-cnf-mysql"> Modify /etc/my.cnf.mysql57</h1><p> sudo sed -i 's/port=3306/port=3307/g' /etc/my.cnf.mysql57<br> sudo sed -i 's#datadir=.*/var/lib/mysql#datadir=/data/mysql57#g' /etc/my.cnf.mysql57</p><h1 id="Modify-etc-my-cnf-mysql"> Modify /etc/my.cnf.mysql80</h1><p> sudo sed -i 's/port=3306/port=3308/g' /etc/my.cnf.mysql80<br> sudo sed -i 's#datadir=.*/var/lib/mysql#datadir=/data/mysql80#g' /etc/my.cnf.mysql80</p><h1 id="Initialize-the-database"> Initialize the database</h1><p> sudo mysql_install_db --user=mysql --datadir=/data/mysql57<br> sudo mysql_install_db --user=mysql --datadir=/data/mysql80</p><h1 id="Start-the-instance-the-commands-need-to-be-adjusted-according-to-your-MySQL-version-and-installation-method"> Start the instance (the commands need to be adjusted according to your MySQL version and installation method)</h1><p> sudo systemctl start mysqld.mysql57<br> sudo systemctl start mysqld.mysql80</p><h1 id="verify"> verify</h1><p> sudo mysql -P 3307 -u root -p<br> sudo mysql -P 3308 -u root -p </p>

Code interpretation and trap evasion

In this code, we use the sed command to modify the configuration file, which is an efficient batch modification method. However, be sure to double-check the modified configuration file to ensure there are no unexpected errors. A small error may cause the instance to fail to start or even the data is corrupted.

In addition, the mysql_install_db command is used to initialize the database, which is a key step in creating a new instance. This command requires root permissions and requires the correct data directory to be specified.

Advanced Tips: System Service Management

To facilitate management, it is recommended to register each MySQL instance as a system service. This way, you can use the systemctl command to start, stop and restart the instance. The specific registration method depends on your Linux distribution and MySQL installation method, please refer to the relevant documentation.

Performance Tuning and Best Practices

Multi-instance deployment consumes a lot of system resources, so resource allocation needs to be planned reasonably. The configuration file of each instance needs to be tuned according to actual conditions, such as adjusting the cache size, limiting the number of connections, etc. At the same time, data must be backed up regularly to prevent data loss. Code specifications and comments are also important, which facilitate future maintenance and upgrades.

Summary: It's not just steps, but also understanding

Remember that deploying multiple instances of MySQL is not just about copying the steps, but more importantly, understanding the principles behind it. Only by understanding the importance of data catalogs, configuration files and system service management can we truly master this technology and be able to deal with various emergencies. I hope this article can help you successfully complete the deployment of MySQL multi-instances and provide some help for your database management path. I wish you all the best!

The above is the detailed content of Detailed explanation of the steps for deploying MySQL multi-instance in Linux environment. For more information, please follow other related articles on the PHP Chinese website!

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
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools