京东“狗场”出品——mysql的应用与解析

80 篇文章 0 订阅
55 篇文章 0 订阅

一个简单的mysql全量备份脚本,备份最近15天的数据。

备份

 
    
1
2
3
4
 
    
#每天备份mysql数据库(保存最近15天的数据脚本)
DATE=$(date +%Y%m%d)
/home/cuixiaohuan /lamp/mysql5 /bin/mysqldump -uuser -ppassword need_db > /home/cuixiaohuan /bak_sql/mysql_dbxx_ $DATE.sql;
find /home/cuixiaohuan /bak_sql/ -mtime + 15 -name '*.sql' -exec rm -rf {} \;

恢复

mysql 数据导入

 
    
1
2
 
    
drop databases need_db;
create databases need_db;

导入数据:必须设定编码进行恢复

 
    
1
 
    
./mysql -uroot -p --default-character-set=utf8 need_db < xx.sql

数据库部署和实践

数据库在实践中,往往需要进行多机主从备份保证安全,这个毋庸置疑;进行读写分离和负载均衡可以极大的提升mysql的读写性能。作者在实践中采用阿里的ameoba进行了读写分离和负载均衡操作。细节步骤参考小拽文章: mysql主从备份,读写分离和负载均衡实践

那么问题来了,部署完了,校验也需慎重,下面是简单的校验过程。

具有1-5工作经验的,面对目前流行的技术不知从何下手,需要突破技术瓶颈的可以加群。在公司待久了,过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的可以加群。如果没有工作经验,但基础非常扎实,对java工作机制,常用设计思想,常用java开发框架掌握熟练的可以加群。java架构群:591240817 一起交流。

php简单读写库脚本

上码:能用代码说的,最好不用文字说话!

 
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
    
<?php
/**
* 进行读写分离的校验
* @notice :需要关闭主从备份的情况下进行
* 原理:打开主从,写主库,从库获取数据,校验主从备份;关闭主从写ameoba,校验读写分离和负载
*
* @author: cuixiaohuan
* Date: 15/12/29
* Time: 下午9:10
*/
class ReadAndWriteTest {
// ameoba 设定端口,校验读写时,放主库配置
const IP = "ip:port";
const PWD = "pwd";
const USER = "user";
const DB = "db";
public function __construct(){
error_reporting(E_ALL ^ E_DEPRECATED);
$this->initDb();
$this->_writeTest();
$this->_selectTest();
}
/**
* 进行10次读操作
*/
public function _selectTest(){
for ($i = 0; $i < 10; $i++) {
$read_sql = 'select * from test limit 10';
$g_result = mysql_query($read_sql);
var_dump($g_result);
mysql_free_result($g_result);
}
}
/**
* 进行10次写操作
*/
public function _writeTest(){
for ($i = 0; $i < 10; $i++) {
$id = uniqid();
$content = "pingce" . uniqid();
$write_sql = 'INSERT INTO `test`(`test`, `test1`) VALUES ("' . $id . '","' . $content . '")';
$g_result = mysql_query($write_sql);
var_dump($g_result);
}
}
/**
* 初始化数据库连接信息 info
*/
private function initDb()
{
$crowd_conn = mysql_pconnect( self::IP, self::USER, self::PWD);
if (!$crowd_conn) {
die( "Could not connect:" . mysql_error());
}
$crowd_db = mysql_select_db( self::DB, $crowd_conn);
}
}
$rw = new ReadAndWriteTest();

主从备份校验

  • 开启slave
  • 调整数据库信息为mysql,主库信息,运行脚本。
  • 查看从库的log,有如下写入操作,说明实时主从备份成功。
     
          
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
          
    151231 15: 36: 21 4 Query start slave
    14 Connect Out pingce@ 10.95.112.120: 3666
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e5c85", "pingce5684d957e5cf2")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e7937", "pingce5684d957e7982")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e8e96", "pingce5684d957e8ee4")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957ea2c2", "pingce5684d957ea2eb")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eb565", "pingce5684d957eb5b3")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957ec7ee", "pingce5684d957ec83e")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eda2f", "pingce5684d957eda78")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eeca4", "pingce5684d957eecf0")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eff16", "pingce5684d957eff61")
    15 Query COMMIT /* implicit, from Xid_log_event */
    15 Query BEGIN
    15 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957f121e", "pingce5684d957f126d")
    15 Query COMMIT /* implicit, from Xid_log_event */

检验读写分离

  • 读写分离,首先需要关闭从机器上的slave。原因:存在主从的话,无法通过log查看出读写分离操作。

     
          
    1
    2
     
          
    mysql> stop slave;
    Query OK, 0 rows affected ( 0.08 sec)
  • 运行脚本:如下信息标示,运行成功。

     
          
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
          
    [cuixiaohuan TestScript]$ /home/work/lamp/php5/bin/php ReadAndWriteTest.php
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957e5c85", "pingce5684d957e5cf2")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957e7937", "pingce5684d957e7982")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957e8e96", "pingce5684d957e8ee4")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957ea2c2", "pingce5684d957ea2eb")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957eb565", "pingce5684d957eb5b3")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957ec7ee", "pingce5684d957ec83e")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957eda2f", "pingce5684d957eda78")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957eeca4", "pingce5684d957eecf0")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957eff16", "pingce5684d957eff61")bool( true)
    INSERT INTO `test`( `test`, `test1`) VALUES ( "5684d957f121e", "pingce5684d957f126d")bool( true)
    resource( 5) of type (mysql result)
    resource( 6) of type (mysql result)
    resource( 7) of type (mysql result)
    resource( 8) of type (mysql result)
    resource( 9) of type (mysql result)
    resource( 10) of type (mysql result)
    resource( 11) of type (mysql result)
    resource( 12) of type (mysql result)
    resource( 13) of type (mysql result)
    resource( 14) of type (mysql result)
  • 查询读写库的log

    解释:之所以主库放一个读写库,是因为有些要求超高一致性的数据,备份可能会有延迟;所以,主库承担读写操作,和高负载。

     
           
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
           
    #读写机器log: 进行了 10次写和 四次读
    151231 15: 29: 27 19 Query set names gbk^@
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e5c85", "pingce5684d957e5cf2")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e7937", "pingce5684d957e7982")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957e8e96", "pingce5684d957e8ee4")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957ea2c2", "pingce5684d957ea2eb")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eb565", "pingce5684d957eb5b3")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957ec7ee", "pingce5684d957ec83e")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eda2f", "pingce5684d957eda78")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eeca4", "pingce5684d957eecf0")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957eff16", "pingce5684d957eff61")
    19 Query INSERT INTO `test`(`test`, `test1`) VALUES ( "5684d957f121e", "pingce5684d957f126d")
    19 Query select * from test limit 10
    151231 15: 29: 28 19 Query select * from test limit 10
    19 Query select * from test limit 10
    19 Query select * from test limit 10
    19 Query select * from test limit 10
  • 查看读库的log

     
          
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
          
    # 只进行了读操作,校正了数据库的读写分离操作。
    151231 15:29:20 4 Query stop slave
    151231 15:29:27 3 Query set names gbk^@
    3 Query select * from test limit 10
    3 Query select * from test limit 10
    3 Query select * from test limit 10
    3 Query select * from test limit 10
    3 Query select * from test limit 10
    3 Query select * from test limit 10

最后

一句话:打开slave,校验主从备份;关闭slave,校验读写分离。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值