Home Database Mysql Tutorial Mysql、MariaDB传统主从集群配置

Mysql、MariaDB传统主从集群配置

Jun 07, 2016 pm 02:56 PM
mariadb mysql Configuration cluster

Mysql和MariaDB的最基本的集群方式是主从方式,也是众多中小企业使用最多的集群方式,该集群部署简单,轻松上手,让我们一步步来吧。 测试所用版本为MariaDB 10.1 源码安装,安装到默认路径/usr/local/mysql下。 $BUILD/autorun.sh$./configure$make-j4$sudo

    Mysql和MariaDB的最基本的集群方式是主从方式,也是众多中小企业使用最多的集群方式,该集群部署简单,轻松上手,让我们一步步来吧。

    测试所用版本为MariaDB 10.1 源码安装,安装到默认路径/usr/local/mysql下。

   

$ BUILD/autorun.sh
$ ./configure
$ make -j4
$ sudo make install
Copy after login


既然是主从数据库,至少需要两个数据库实例,本文例子将两个实例部署在同一台电脑里,计划配置如下表。

实例作用
地址
端口
数据文件路径
配置文件路径

localhost
10001
/home/lyw/db/data/1
/home/lyw/db/etc/my1.cnf

localhost
10002
/home/lyw/db/data/2/home/lyw/db/etc/my2.cnf

准备两个mysql的配置文件my1.cnf, 我们从默认配置复制过来

$ mkdir -p /home/lyw/db/etc/
$ cd /usr/local/mysql
$ cp support-files/my-medium.cnf /home/lyw/db/etc/my1.cnf
$ cp support-files/my-medium.cnf /home/lyw/db/etc/my2.cnf
Copy after login


my1.cnf修改如下几行

[mysqld]
port            = 10001
socket          = /tmp/mysql1.sock
log-bin         = mysql-bin
server-id       = 10001
datadir        = /home/lyw/db/data/1
user            = lyw
Copy after login

my2.cnf修改如下几行

[mysqld]
port            = 10002
socket          = /tmp/mysql2.sock
log-bin         = mysql-bin
server-id       = 10002
datadir        = /home/lyw/db/data/2
user            = lyw
Copy after login

分别初始化两个数据库

$ mkdir -p /home/lyw/db/data/
$ scripts/mysql_install_db --defaults-file=/home/lyw/db/etc/my1.cnf
$ scripts/mysql_install_db --defaults-file=/home/lyw/db/etc/my2.cnf
Copy after login

启动两个数据库

由于是在一台电脑里,因此没有使用服务启动,而是直接用mysqld_safe启动,大家尽可根据自己的情况启动。

$ bin/mysqld_safe --defaults-file=/home/lyw/db/etc/my1.cnf  &
$ bin/mysqld_safe --defaults-file=/home/lyw/db/etc/my2.cnf  &
Copy after login

检查两个数据库是否启动成功,对应的端口和sock文件已经存在,即表示启动成功。

$ sudo netstat -nlp |grep mysql
tcp6       0      0 :::10001                :::*                    LISTEN      22211/mysqld    
tcp6       0      0 :::10002                :::*                    LISTEN      23536/mysqld    
unix  2      [ ACC ]     STREAM     LISTENING     365439   22211/mysqld        /tmp/mysql1.sock
unix  2      [ ACC ]     STREAM     LISTENING     368511   23536/mysqld        /tmp/mysql2.sock
Copy after login

MariaDB默认有两行任何本机用户都能登陆,会影响新创建的复制用户的登陆,因此需要删除。然后创建复制用户方可生效。(如果是在两台不同的电脑中运行,可以不删除这两行。)

$ bin/mysql -S /tmp/mysql1.sock -uroot
MariaDB [(none)]> use mysql;
MariaDB [mysql]> delete from user where user='';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> grant replication slave on *.* to 'rep'@'%' identified by '123456';
Copy after login

然后查看master数据库的运行状态,记下这两个值。

MariaDB [mysql]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |     1075 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Copy after login

在从服务器上执行

$ bin/mysql -S /tmp/mysql2.sock -uroot
MariaDB [(none)]> change master to 
master_host='localhost', 
master_port=10001, 
master_user='rep', 
master_password='123456', 
master_log_file='mysql-bin.000005', 
master_log_pos=1075;

MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: localhost
                  Master_User: rep
                  Master_Port: 10001
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 1075
               Relay_Log_File: lyw-hp-relay-bin.000002
                Relay_Log_Pos: 537
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
Copy after login

如果Slave_IO_Running 和 Slave_SQL_Running 都为Yes,则说明启动主从配置成功。


测试:

在master数据库执行

MariaDB [mysql]> create database lyw;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lyw                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
Copy after login

在slave数据库执行

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lyw                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
Copy after login

可见在主库创建的数据库lyw,在从库也有。可以在主库尝试执行其他写入语句,在丛库也能看到同样的数据。


用同样的方法,一个主数据库后面可以跟多个从数据库,增加数据的可靠性和读的吞吐量。

如果主库也配置上从从库同步数据,即互为主从,那就是常说的双主,或主主,有些文章里会说修改下自增涨id以防止冲突,我却不这样认为,双主的两个数据库并非强一致,而是有一定的延迟,很多事务处理会出现错误,皆因为延迟导致。双主并不能大大增强写入的吞吐量,因为所有数据都要在每个库写入。虽然说因为批量的缘故可以有少量提升,但是对于延迟导致的错误来说,微不足道。

配置双主是有意义的,但是并非用于同时写入,同一时间还是只写一个库,而当主挂了后,为切换主从做的准备,切换后,主再次启动时,立马就能同步到从的数据。比如在cobar集群中,就可以考虑用双主配置。


以上是传统主从复制方法,从Mysql5.6和MariaDB10.0开始,有了新的主从复制方式,可参考这篇文章:http://bangbangba.blog.51cto.com/3180873/1702294 

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 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

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles