aws mysql_更改 AWS RDS mysql时区

AWS RDS

AWS上搭建数据库的时候,不是DB on EC2就是RDS,但是选择RDS时,Timezone怎么处理?

「面向全球提供的AWS来讲理所当然的是UTC」,而RDS也不是例外。把服务器迁移到AWS时,「数据库能不能使用中国时间」是常见的一个问题。 DB on EC2的话,配置一下系统的Timezone就可以,但是RDS是我们无法直接登录的因此需要使用MySQL的功能来实现。

介绍如何修改RDS MySQL的Timezone。

在RDS的Master用户不同于MySQL root用户,因此没有SUPER权限(管理者权限)。因此不能使用SET GLOBAL命令修改Timezone。在这里使用MySQL的init_connect参数里使用SET SESSION命令来修改Timezone。Init_conect参数实在Parameter Group里进行修改。

注意事项

rdsadmin用户貌似是AWS用来管理RDS实例的用户,无法判断影响范围因此不修改rdsadmin的Timezone。

在init_connect参数里直接填写命令会无法正常运行,因此定义Stored Procedure,用CALL方式执行

操作流程如下

创建Stored Procedure

创建Parameter Group

Parameter Grouup关联到RDS

重启RDS

1.创建Stored Procedure

以Master用户登录到RDS,shared Schema创建Stored Procedure(shared.store_time_zong)。

把时间配置为中国标准时间(Asia/Chongqing)。

mysql> CREATE DATABASE shared;

Query OK, 1 row affected (0.00 sec)

mysql> DELIMITER |

mysql> CREATE PROCEDURE shared.`store_time_zone`()

-> IF (POSITION('aws_rds@' IN CURRENT_USER()) = 1) THEN

-> SET SESSION time_zone = 'Asia/Chongqing';

-> END IF |

Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;

1

2

3

4

5

6

7

8

9

10

11

mysql>CREATEDATABASEshared;

QueryOK,1rowaffected(0.00sec)

mysql>DELIMITER|

mysql>CREATEPROCEDUREshared.`store_time_zone`()

->IF(POSITION('aws_rds@'INCURRENT_USER())=1)THEN

->SETSESSIONtime_zone='Asia/Chongqing';

->END IF|

QueryOK,0rowsaffected(0.01sec)

mysql>DELIMITER;

确认Stored Procedure好不好用

mysql> select now();

+---------------------+

| now() |

+---------------------+

| 2015-04-09 05:10:41 |

+---------------------+

1 row in set (0.00 sec)

mysql> CALL shared.store_time_zone;

Query OK, 0 rows affected (0.07 sec)

mysql> select now();

+---------------------+

| now() |

+---------------------+

| 2015-04-09 13:10:52 |

+---------------------+

1 row in set (0.00 sec)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

mysql>selectnow();

+---------------------+

|now()|

+---------------------+

|2015-04-0905:10:41|

+---------------------+

1rowinset(0.00sec)

mysql>CALLshared.store_time_zone;

QueryOK,0rowsaffected(0.07sec)

mysql>selectnow();

+---------------------+

|now()|

+---------------------+

|2015-04-0913:10:52|

+---------------------+

1rowinset(0.00sec)

确认到时间变为中国标准时间(比UTC快8小时)。

2.创建Parameter Group

RDS上是以Parameter Group的形式来管理,MySQL的参数,在这里不修改默认的Parameter Group,创建一个Parameter Group。

点击:Parameter Groups

点击:Create Parameter Group

fa48141302c27d834166bf05d38cdf90.png

Parameter Group Family:选择mysql5.6(因为我们的RDS MySQL版本是5.6.22)

Group Name:timezone-Chongqing

Description:init_connect call store_time_zone

597de075128e5d23f16c24e5b0f9159c.png

选择:timezone-chongqing

点击:Edit Parameters

d15eabb9e88dea681dc1c15faa99e680.png

init_connect:CALL shared.store_time_zone

7178fc15e5cc408dfa52a3dde94af85a.png

点击:Save Changes

67f9150e746456502551d76c60abd2c3.png

选择:RDS实例(awspack)

点击:Instance Actions

点击:Modify

edd61857217d0751a6a3aa0d9799caad.png

DB Parameter Group:timezone-chongqing

点击:Continue

369fc540f0b446cc3730a94710d326b5.png

确认:DB Parameter Group为timezone-chongqing

点击:Modify DB Instance

87f7bd48296075a9596753fb1d2fb998.png

当用户连接到RDS时,通过init_connect调用Stored Procedure。

3. 重启RDS

重启RDS实例之前确认一下,Parameter Group的状态在applying。

点击:RDS Dashboard的Instances

选择:RDS实例awspack

点击:Instance Actions

点击:Reboot

9553b65af911dacf5acd9177fc50f999.png

重启RDS实例之后确认,Parameter Group的状态为in-sync

853cb8657e05572886333f6045c34caf.png

以aws_rds用户登录到MySQL以后查看,时间为中国时间。

$ mysql -h awspack.crhydmkxhg6d.ap-northeast-1.rds.amazonaws.com -uaws_rds -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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 global variables like 'init_connect';

+---------------+-----------------------------+

| Variable_name | Value |

+---------------+-----------------------------+

| init_connect | CALL shared.store_time_zone |

+---------------+-----------------------------+

1 row in set (0.00 sec)

mysql> select now();

+---------------------+

| now() |

+---------------------+

| 2015-04-09 13:44:10 |

+---------------------+

1 row in set (0.00 sec)

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

$mysql-hawspack.crhydmkxhg6d.ap-northeast-1.rds.amazonaws.com-uaws_rds-p

Enterpassword:

WelcometotheMySQLmonitor.Commandsendwith;or\g.

YourMySQLconnectionidis7

Serverversion:5.6.22-logMySQLCommunityServer(GPL)

Copyright(c)2000,2015,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/orits

affiliates.Othernamesmaybetrademarksoftheirrespective

owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>showglobalvariableslike'init_connect';

+---------------+-----------------------------+

|Variable_name|Value|

+---------------+-----------------------------+

|init_connect|CALLshared.store_time_zone|

+---------------+-----------------------------+

1rowinset(0.00sec)

mysql>selectnow();

+---------------------+

|now()|

+---------------------+

|2015-04-0913:44:10|

+---------------------+

1rowinset(0.00sec)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值