Linux 搭建Mysql主从节点复制
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主从节点复制的更多相关文章
- Linux搭建MySQL主从
实现目标 搭建两台MySQL服务器(一主一从),一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作. 工作流程概述 主服务器: 开启二进制日志 配置唯一的server-id 获 ...
- linux下搭建mysql主从
在master上创建repl账户,用于复制. grant replication slave on *.* to 'repl'@'%' identified by 'P@$$W0rd'; flush ...
- 怎么使用mysqlreplicate快速搭建MySQL主从呢?
用其中的mysqlreplicate工具来快速搭建MySQL主从环境. HE1:192.168.1.248 slave HE3:192.168.1.250 master 实战 Part1:安装mysq ...
- linux中MySQL主从配置(Django实现主从读写分离)
一 linux中MySQL主从配置原理(主从分离,主从同步) mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到sl ...
- Docker - Docker中搭建MySQL主从
1.pull完centos7纯净版的镜像后,创建容器,然后将宿主机上下载的MySQL文件 (MySQL下载地址:http://mysql.mirror.kangaroot.net/Downloads/ ...
- linux系统mysql主从配置
一.原理 mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到slave; 3) slave有一个I/O线程把mast ...
- 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境
应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...
- 2-18,19 搭建MySQL主从服务器并并通过mysql-proxy实现读写分离
MySQL主从服务器 实现方式: MySQL REPLICATION Replication可以实现将数据从一台数据库服务器(master)复制到一台或多台数据库服务器(slave) 默认情况下这种 ...
- linux 下 mysql 主从配置
话不多说,直接干. 准备条件:安装两个mysql数据库,随便哪个作主库,另一个从库. 1.在主库创建 复制用的账号 grant replication slave ,replication clien ...
随机推荐
- 区间dp 整数划分问题
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- C#面向对象20 序列化和反序列化
序列化和反序列化 序列化是把一个内存中的对象的信息转化成一个可以持久化保存的形式,以便于保存或传输,序列化的主要作用是不同平台之间进行通信,常用的有序列化有json.xml.文件等 一.序列化为j ...
- devXpress ribbonForm处理
1.图标处理 这个图标是通过 Element Ribbon API
- centos7安装nginx服务
Nginx发音引擎x是一个免费的开源高性能HTTP和反向代理服务器,负责处理互联网上一些最大的网站的负载. 本教程将教你如何在你的CentOS Linux 7.5机器上安装和管理Nginx. 安装Ng ...
- cpu 100%怎样定位
先用top定位最耗cpu的java进程 例如: 12430工具:top或者 htop(高级)方法:top -c 显示进程运行详细列表键入 P (大写P),按照cpu进行排序 然后用top -p 124 ...
- ES6基础之——get 与 set
class Chef{ constructor(food){ this.food = food; thid.dish = []; } //getter get menu(){ return this. ...
- element table 表格 修改背景为透明并去除边框
.el-table{ /* 表格字体颜色 */ color:white; /* 表格边框颜色 */ /* border: 0.5px solid #758a99; */ height: 500px; ...
- 第六篇.文件处理之python2和3字符编码的区别
目录 python2和3字符编码的区别 一.字符编码应用之python python2和3字符编码的区别 一.字符编码应用之python 1执行python的三个阶段 python test.py 执 ...
- 【转】草根老师的 linux字符设备驱动详解
Linux 驱动 之 模块化编程 Linux 驱动之模块参数和符号导出 Linux 设备驱动之字符设备(一) Linux 设备驱动之字符设备(二) Linux 设备驱动之字符设备(三)
- 【2017-05-04】winfrom进程、线程、用户控件
一.进程 一个进程就是一个程序,利用进程可以在一个程序中打开另一个程序. 1.开启某个进程Process.Start("文件缩写名"); 注意:Process要解析命名空间. 2. ...