Linux环境 Centos 6.6 64位

准备两台服务器,搭建一主一从,实现Mysql的读写分离和数据备份

主节点 192.168.43.10 leader

从节点 192.168.43.20 db

安装包 mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz(该包含有NDB和Mysql软件)

MySQL主从复制的优点:
1、 如果主服务器出现问题, 可以快速切换到从服务器提供的服务,保证高可用性
2、 可以在从服务器上执行查询操作, 降低主服务器的访问压力
3、 可以在从服务器上执行备份, 以避免备份期间影响主服务器的服务

1.现在两台服务器上安装好版本一样的Mysql,这里演示Leader端的安装,db跳过

1.1解压安装包到路径/usr/local/mysql

[root@leader vm]# tar -zvxf mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz

[root@leader vm]# mv mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64 /usr/local/mysql

1.2创建mysql用户和组以及存放数据的路径

[root@leader mysql]# groupadd mysql
[root@leader mysql]# useradd -r -g mysql mysql

1.3进入到mysql目录,执行添加MySQL配置的操作

创建my.cnf文件,加入如下内容

[root@leader ~]# vi /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#skip-grant-tables
socket = /tmp/mysql.sock
character-set-server = utf8
skip-name-resolve

1.4授权相关路径给用户Mysql

[root@leader mysql]# chown -R mysql .
[root@leader mysql]# chgrp -R mysql .
[root@leader mysql]# chown -R root .
[root@leader mysql]# mkdir data
[root@leader mysql]# chown -R mysql data

1.5初始化数据(在mysql/bin或者mysql/scripts下有个 mysql_install_db 可执行文件初始化数据库),进入mysql/bin或者mysql/scripts目录下,执行下面命令

[root@leader bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2019-11-23T12:10:15.096248Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-23T12:10:15.096315Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2019-11-23T12:10:15.096320Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2019-11-23T12:10:16.643511Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-11-23T12:10:16.904240Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-11-23T12:10:17.034430Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 36285a17-0dea-11ea-908d-000c298eae92.
2019-11-23T12:10:17.039576Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-11-23T12:10:17.042792Z 1 [Note] A temporary password is generated for root@localhost: 8Ns2os:NFde-

1.6添加mysqld服务

[root@leader bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@leader bin]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
[root@leader bin]# chmod 775 /etc/init.d/mysql
[root@leader bin]# chkconfig --add mysqld

[root@leader bin]# chkconfig --level 2345 mysqld on
[root@leader bin]# chown mysql.mysql -R /usr/local/mysql

[root@leader ~]# ln  -s /usr/local/mysql/bin/mysql  /usr/bin--添加mysql命令

1.7 启动mysqld服务,并登陆mysql

[root@leader ~]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/leader.err'.
. SUCCESS!
[root@leader ~]# clear
[root@leader ~]# service mysqld status
SUCCESS! MySQL running (22899)

[root@leader ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27-ndb-7.6.11-cluster-gpl

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'duan' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> Flush privileges;
Query OK, 0 rows affected (0.01 sec)

2.先登录主机 leader,在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE(从复制)赋予权限,
mysql>GRANT REPLICATION SLAVE ON *.* TO 'db'@'192.168.43.20' IDENTIFIED BY '123456'

2.1打开主机leader的my.cnf,输入如下:(修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启mysql服务)

server-id =1 #主机标示,整数
log_bin = /var/log/mysql/mysql-bin.log #确保此文件可写,开启bin-log
read-only =0 #主机,读写都可以
binlog-do-db =test #需要备份数据,多个写多行
binlog-ignore-db =mysql #不需要备份的数据库,多个写多行

2.1创建binlog路径并授权mysql,重启mysqld服务

[root@leader log]# mkdir mysql

[root@leader log]# chown -R mysql.mysql /var/log/mysql

2.3现在可以停止主数据的的更新操作,并生成主数据库的备份,我们可以通过mysqldump导出数据到从数据库,当然了,你也可以直接用cp命令将数据文件复制到从数据库去,注意在导出数据之前先对主数据库进行READ LOCK,以保证数据的一致性:

mysql> create database test;
Query OK, 1 row affected (0.05 sec)

mysql> use test;
Database changed
mysql> create table test_aaa as select * from user;
ERROR 1146 (42S02): Table 'test.user' doesn't exist
mysql> create table test_aaa as select * from mysql.user;
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

[root@leader bin]# /usr/local/mysql/bin/mysqldump -p3306 -uroot -p test>/data/backup/test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Enter password:

将备份文件传到从节点,在从节点创建test库进行恢复;

[root@db bin]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.07 sec)

mysql> exit
Bye
[root@db bin]# mysql -uroot -proot test</data/backup/test.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

2.4得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复。

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 4383 | test | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

2.5修改从数据库的my.cnf,增加server-id参数。打开从机db的my.cnf,输入(修改之后必须重启mysql服务)

server-id = 2
relay-log=mysql-relay #打开relaylog
binlog-do-db =test #需要备份数据,多个写多行
binlog-ignore-db =mysql #不需要备份的数据库,多个写多行

2.6登陆从库,指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置

[root@db local]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO master_host = '192.168.43.10',
-> master_user = 'db',
-> master_password = '123456',
-> master_log_file = 'mysql-bin.000003',
-> master_log_pos = 154;
Query OK, 0 rows affected, 2 warnings (0.10 sec)

mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.43.10
Master_User: db
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes    #为Yes表示配置成功
Slave_SQL_Running: Yes  #为Yes表示配置成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 36285a17-0dea-11ea-908d-000c298eae92
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)

ERROR:
No query specified

3.测试主从服务器是否能同步

主节点

[root@leader data]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.27-ndb-7.6.11-cluster-gpl-log MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table test1 as select * from mysql.user;
Query OK, 5 rows affected (0.13 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test2 as select * from mysql.user;
Query OK, 5 rows affected (0.09 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test3 as select * from mysql.user;
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test4 as select * from mysql.user;
Query OK, 5 rows affected (2.85 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test5 as select * from mysql.user;
Query OK, 5 rows affected (0.07 sec)
Records: 5 Duplicates: 0 Warnings: 0

从节点:

[root@db local]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
| test2 |
| test3 |
| test4 |
| test5 |
| test_aaa |
+----------------+
6 rows in set (0.00 sec)

Linux 搭建Mysql主从节点复制的更多相关文章

  1. Linux搭建MySQL主从

    实现目标 搭建两台MySQL服务器(一主一从),一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作. 工作流程概述 主服务器: 开启二进制日志 配置唯一的server-id 获 ...

  2. linux下搭建mysql主从

    在master上创建repl账户,用于复制. grant replication slave on *.* to 'repl'@'%' identified by 'P@$$W0rd'; flush ...

  3. 怎么使用mysqlreplicate快速搭建MySQL主从呢?

    用其中的mysqlreplicate工具来快速搭建MySQL主从环境. HE1:192.168.1.248 slave HE3:192.168.1.250 master 实战 Part1:安装mysq ...

  4. linux中MySQL主从配置(Django实现主从读写分离)

    一 linux中MySQL主从配置原理(主从分离,主从同步) mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到sl ...

  5. Docker - Docker中搭建MySQL主从

    1.pull完centos7纯净版的镜像后,创建容器,然后将宿主机上下载的MySQL文件 (MySQL下载地址:http://mysql.mirror.kangaroot.net/Downloads/ ...

  6. linux系统mysql主从配置

    一.原理 mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到slave; 3) slave有一个I/O线程把mast ...

  7. 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境

    应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...

  8. 2-18,19 搭建MySQL主从服务器并并通过mysql-proxy实现读写分离

    MySQL主从服务器 实现方式: MySQL  REPLICATION Replication可以实现将数据从一台数据库服务器(master)复制到一台或多台数据库服务器(slave) 默认情况下这种 ...

  9. linux 下 mysql 主从配置

    话不多说,直接干. 准备条件:安装两个mysql数据库,随便哪个作主库,另一个从库. 1.在主库创建 复制用的账号 grant replication slave ,replication clien ...

随机推荐

  1. SIP笔记

    消息代号: 1)1XX:临时响应,表示请求消息正在被处理. 2)2XX:成功响应,表示请求已被成功接收,完全理解并被接受. 3)3XX:重定向响应,表示需采取进一步以完成该请求. 4)4XX:客户机错 ...

  2. luogu P3645 [APIO2015]雅加达的摩天楼

    luogu 暴力? 暴力! 这个题有点像最短路,所以设\(f_{i,j}\)表示在\(i\)号楼,当前\(doge\)跳跃能力为\(j\)的最短步数,转移要么跳一步到\(f_{i+j,j}\)和\(f ...

  3. 转载Linux常用命令

    转自:https://blog.csdn.net/deng_xj/article/details/88803148 Linux常用shell命令 [root@dengxj]#各项含义[用户名@计算机名 ...

  4. Java学习笔记【六、正则表达式】

    参考:http://www.runoob.com/java/java-regular-expressions.html 概述 java.util.regex包,主要包含三个类: Pattern:正则表 ...

  5. 第十章、os模块

    目录 第十章.os模块 一.os模块 第十章.os模块 一.os模块 方法 详解 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirn ...

  6. 学习--Spring IOC源码精读

    Spring核心IOC的源码分析(转载) 原文地址:https://javadoop.com/post/spring-ioc#toc11 转载地址:https://blog.csdn.net/nuom ...

  7. yolo模型的特点与各版本性能对比

    目录 一.YOLOV1 二.YOLOV2 二.YOLOV3 正文 目前,基于深度学习的目标检测算法大致可以分为两大流派: 1.两阶段(two-stage)算法:先产生候选区域然后再进行CNN分类(RC ...

  8. 《python解释器源码剖析》第7章--python中的set对象

    7.0 序 集合和字典一样,都是性能非常高效的数据结构,性能高效的原因就在于底层使用了哈希表.因此集合和字典的原理本质上是一样的,都是把值映射成索引,通过索引去查找. 7.1 PySetObject ...

  9. MySQL单表查询实例

    数据表准备 ​```mysql create table emp( id int not null unique auto_increment, name varchar(20) not null, ...

  10. (十)全志R18 Tina平台关闭所有串口打印的方法

    全志R18 Tina平台关闭所有打印输出方法: 有些国外的产品安全认证,如亚马逊Alexa认证,认证机构会不停地点pcb上的点,看有没有东西输出,有的话就通过这些口想办法破解设备,所以安全认证会要求设 ...