Home > Database > Mysql Tutorial > body text

Mysql、MariaDB传统主从集群配置

WBOY
Release: 2016-06-07 14:56:16
Original
982 people have browsed it

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 

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!