在高訪问量服务环境下,单机配置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. MEF and AppDomain z

    MEF and AppDomain - Remove Assemblies On The Fly This article will give an idea of what's involved i ...

  2. windows 下运行 UglifyJS

    Node 0.52版本支持windows,因此UglifyJS可以在windows上运行,下面简介他的方法: 1.下载node.exe,现在最新版本0.52,他的官网http://nodejs.org ...

  3. MySql排名查询

    1.新建一张成绩表 -- 新建成绩表 CREATE TABLE IF NOT EXISTS `score` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT ...

  4. 非常精简的Linux线程池实现(一)——使用互斥锁和条件变量

    线程池的含义跟它的名字一样,就是一个由许多线程组成的池子. 有了线程池,在程序中使用多线程变得简单.我们不用再自己去操心线程的创建.撤销.管理问题,有什么要消耗大量CPU时间的任务通通直接扔到线程池里 ...

  5. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  6. 异常捕获 UncaughtExceptionHandler MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. Packagist 镜像使用方法--composer

    镜像用法 有两种方式启用本镜像服务: 系统全局配置: 即将配置信息添加到 Composer 的全局配置文件 config.json 中.见“方法一” 单个项目配置: 将配置信息添加到某个项目的 com ...

  8. MySQL数据库select语句的使用方法

    select语句可 以用回车分隔 $sql="select * from article where id=1"和 $sql="select * from article ...

  9. intel 汇编中断解释

    汇编中的10H中断是由BIOS对显示器和屏幕所提供的服务程序.使用int 10h服务程序时,必须先指定ah寄存器为以下显示服务编号之一,以指定需要调用的功用. 显示服务 (Video Service: ...

  10. Mysql写入中文出错

    本地调试好像正常,服务器运行报错: UnicodeEncodeError: 'latin-1' codec can't encode character u'\u5206' in position 2 ...