在高訪问量服务环境下,单机配置mysql服务将无法满足频繁快速的数据读写操作。

一旦mysql出现故障造成数据丢失。无法恢复。

因此。在mysql服务上启用主从备份功能,支持读写分离技术。最靠可的是搭建负载均衡分布式数据库系统,更加可靠、稳定。

1、搭建好开发环境

两台centos机器,安装mysql服务以及其它依赖包。一台是主server(master),还有一台是从server(salve)。本节下面操作在两台server都运行一遍:

192.168.213.135(master)
192.168.213.136(salver)

安装mysql,命令行输入:

yum install mysql*

等待安装结束。启动mysql服务:

[liang@localhost Desktop]$ sudo service mysqld restart

打印例如以下即启动成功:

[sudo] password for liang:
Stopping mysqld: [ OK ]
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password' Alternatively you can run:
/usr/bin/mysql_secure_installation which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ]
Starting mysqld: [ OK ] [liang@localhost Desktop]$ sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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>

更改mysql管理员默认空password。mysql命令行输入:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');

打印例如以下即更改password成功:

Query OK, 0 rows affected (0.00 sec)

退出数据库:

mysql> quit

又一次使用新的passwordroot权限登录数据库:

[liang@localhost Desktop]$ sudo mysql -uroot -p
Enter password:

输入password再次进入mysql管理client界面加入mysql用户:

mysql> CREATE USER liang IDENTIFIED BY '123456';

为新用户创建数据库:

mysql> create database liang;

设置用户登录、操作权限,同意本机以liang账号登陆,操作liang数据库的所用表:

mysql> grant all privileges on liang.* to liang@localhost identified by '123456';

此时,退出mysql的管理员账号,是liang账号登陆,查看liang账号能否够操作liang数据库,liang登录数据库,登录成功:

[liang@localhost html]$ sudo mysql -uliang -p
[sudo] password for liang:

登录成功打印例如以下:

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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.

查看可用数据库。能够看到liang数据:

mysql> show databases;

打印例如以下包含前面加入的liang数据库:

+--------------------+
| Database |
+--------------------+
| information_schema |
| liang |
| test |
+--------------------+
3 rows in set (0.00 sec)

选择使用liang数据库:

mysql> use liang;

创建一个数据库表:

mysql> create table osinfo (id int primary key,os char(20),ttl int check(ttl>0));

插入数据:

mysql> insert into osinfo(id,os,ttl) values(1,'win10',128);
mysql> insert into osinfo(id,os,ttl) values(2,'ubuntu15',64);

查询数据表:

mysql> select * from osinfo;

打印例如以下信息,出现前面插入数据项:

+----+----------+------+
| id | os | ttl |
+----+----------+------+
| 1 | win10 | 128 |
| 2 | ubuntu15 | 64 |
+----+----------+------+
2 rows in set (0.00 sec)

2、配置master

编辑数据库配置文件。命令行输入:

[root@localhost Desktop]# vi /etc/my.cnf

加入内容,保存退出:

##############################
server-id = 1
log-bin=mysql-bin
binlog-do-db = liang
binlog-ignore-db = mysql,test,information_schema
##############################

重新启动mysql服务:

[root@localhost Desktop]# service mysqld restart

打印例如以下信息。即配置文件没有出错。又一次启动mysql成功:

Stopping mysqld:                                           [  OK  ]
Starting mysqld:

master授权从slave主从备份权限,root权限登录servermysql,输入下面命令:

mysql> grant replication slave on *.* to 'liang'@'192.168.213.136' identified by '123456';

刷新权限列表,使刚才配置权限生效:

mysql> flush privileges;

从slave測试远程登陆master上的mysql:

[root@localhost Desktop]# mysql -h 192.168.213.135 -u liang -p

输入password登录成功打印例如以下。拥有了远程备份的权限:

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, 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>

3、配置slave

配置数据库配置文件。命令行输入例如以下:

[root@localhost Desktop]# vi /etc/my.cnf

加入内容,保存退出:

#################################
server-id=2
master-host= 192.168.213.135
master-port=3306
master-user=liang
master-password=123456
replicate-do-db=liang
master-retry-count = 999
master-connect-retry = 60
#################################

重新启动mysql服务:

[root@localhost Desktop]# service mysqld restart

打印例如以下内容即重新启动服务成功:

Stopping mysqld:                                           [  OK  ]
Starting mysqld: [ OK ]

4、验证主从备份状态

查看master状态。master主机上。root权限登录mysql,输入例如以下sql语句:

mysql> show master status;

打印以下内容。File和Position相应的值,以后能够配置slave用于master_log_file、master_log_pos。刚安装的mysql能够跳过:

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: *** NONE *** +------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 | 106 | liang | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)

查看slave状态,slave主机上,root权限登录mysql,输入例如以下sql语句:

mysql> show slave status;

打印例如以下内容:

+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error |
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
| Waiting to reconnect after a failed master event read | 192.168.213.135 | liang | 3306 | 60 | | 4 | mysqld-relay-bin.000001 | 4 | | No | Yes | liang | | | | | | 0 | | 0 | 0 | 106 | None | | 0 | No | | | | | | NULL | No | 0 | | 0 | |
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+
1 row in set (0.00 sec)

Slave_SQL_Running、Slave_IO_Running 为 YES 即主从备份开启。可是这里Slave_IO_Running为 NO,所以如今主从备份并没有开启。两种原因造成当前错误:程序可能在slave上进行了写操作。也可能是slave机器重起后,事务回滚造成的,通常是后者。

尝试上面的解决方法,命令行输入。验证主从备份功能:

mysql> show slave status\G;

打印例如以下,Slave_IO_Running: Yes,Slave_SQL_Running: Yes,基本配置成功:

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.213.135
Master_User: liang
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 382
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: liang
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: 382
Relay_Log_Space: 828
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:
1 row in set (0.00 sec)

5、防火墙配置

master命令行打开tcp的3306port:

iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

slave命令行打开tcp、udp的3306port:

iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p udp --dport 3306 -j ACCEPT

6、測试主从备份功能

主server上使用liang用户在liang数据库中插入新数据:

mysql> insert into osinfo(id,os,ttl) values(3,'win7',128);

主server上查询数据::

mysql> select * from osinfo;

出现上面插入的数据,即插入数据成功:

+----+----------+------+
| id | os | ttl |
+----+----------+------+
| 2 | ubuntu15 | 64 |
| 1 | win10 | 128 |
| 3 | win7 | 128 |
+----+----------+------+
3 rows in set (0.00 sec)

从server上查询数据:

mysql> select * from osinfo;

出现上面插入的数据。即主从备份功能配置成功:

+----+----------+------+
| id | os | ttl |
+----+----------+------+
| 2 | ubuntu15 | 64 |
| 1 | win10 | 128 |
| 3 | win7 | 128 |
+----+----------+------+
3 rows in set (0.00 sec)

mysql主从备份功能配置与測试的更多相关文章

  1. MySQL主从备份配置实例

    转载自:https://www.cnblogs.com/ahaii/p/6307648.html MySQL主从备份配置实例 场景: 1.主服务器192.168.0.225.从服务器192.168.0 ...

  2. MySQL主从备份配置

    MySQL主从热备配置 两台服务器的MySQL版本都是5.5.41master:192.168.3.119slave:192.168.3.120 MySQL主服务器配置:1.创建用于备份的用户 gra ...

  3. mysql主从备份及常见问题处理

    1.mysql主从备份基本原理 mysql支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.mysql复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新 ...

  4. MYSQL主从库同步配置过程

    MYSQL主从库同步配置过程 为了实现网站数据库的异地备份,采用了MySQL数据库主从同步配置,需要两台服务器分别作为主从库,当主库发生增删改等操作,会实时反映到从库,我的个人服务器配置如下: 主库为 ...

  5. mysql主从备份及原理分析

    一.mysql主从备份(复制)的基本原理mysql支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.mysql复制基于主服务器在二进制日志中跟踪所有对数据库的更 ...

  6. 检测MySQL主从备份是否运行

    通过查看 slave  状态,确保 Slave_IO_Running: Yes Slave_SQL_Running: Yes #!/bin/bash#Author:Darius-Dmysql -uro ...

  7. django+centos+mariadb读写分离完美实现(上)-mysql主从备份实现

    首先画图一张,用来展示今天要做的事情,读写分离,个人理解就是使用mysql主从备份的原理,让两个数据库同时为自己提供服务.其中主库负责数据保存,从库负责数据展示,可以一主一从,也可以一主多从.从而降低 ...

  8. linux下 mysql主从备份

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/czh0423/article/details/26720539 一.准备 用两台server做測试: ...

  9. 【实操笔记】MySQL主从同步功能实现

    写在前边: 这两天来了个需求,配置部署两台服务器的MySQL数据同步,折腾了两天查了很多相关资料,一直连不上,后来发现其实是数据库授权的ip有问题,我们用的服务器是机房中的虚拟机加上反向代理出来的,坑 ...

随机推荐

  1. python测试开发django-27.表单提交之post修改密码

    前言 跟账号相关的功能一般是注册,登录,修改密码,密码找回功能,前面实现了登录和注册功能,本篇讲下修改密码功能实现 修改密码html <!DOCTYPE html> <html la ...

  2. Java CMYK图片转RGB图片(TwelveMonkeys方式)

    TwelveMonkeys的使用比较简单,只要把相关的jar包加入到类路径,他的类我们基本不会用到,只要使用jdk ImageIO或其上层的接口就行了.jdk的ImageIO有自动发现功能,会自动查找 ...

  3. java nio 映射文件内容到内存

    FileChannel 的一个最好的功能就是能将文件的某个部分直接映射到内存.这要感谢 FileChannel.map() 方法,这个方法有下面三个参数: mode:映射到内存需要指定下面三种模式之一 ...

  4. UICollectionViewFlowLayout使用示例

    UICollectionViewFlowLayout使用示例 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples // // ViewC ...

  5. 冰血暴第一季/全集Fargo迅雷下载

    冰血暴 第一季 Fargo 1 (2014)本季看点: 该剧改编自科恩兄弟获得1996年奥斯卡提名的同名经典影片,计划总共拍摄10集,第一季将讲述一个完整的故事.由<识骨寻踪第一季>编剧诺 ...

  6. docker 查看容器的网络连接

    #! /bin/bash echo $1 PID=$(docker inspect -f '{{.State.Pid}}' $1) nsenter -t $PID -n netstat |grep E ...

  7. macOS Sierra10.12.5 显示允许任何来源

    在终端输入:sudo spctl --master-disable即可.

  8. Elasticsearch 入门教程

    全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选. 它可以快速地储存.搜索和分析海量数据.维基百科.Stack Overflow.Gi ...

  9. JavaScript Math和Number对象研究

    1. Math 对象 1.1 介绍   Math 对象,是数学对象,提供对数据的数学计算,如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法.   1.2 构造函数   无 : ...

  10. 学界 | Yann LeCun新作,中日韩文本分类到底要用哪种编码?

    https://www.wxwenku.com/d/102093756 AI科技评论按:前几天,Yann LeCun与其学生 张翔在arXiv上发表了一篇新作「Which Encoding is th ...