首页域名资讯 正文

更改 AWS RDS mysql时区

2024-11-25 3 0条评论

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方式执行

操作流程如下

  1. 创建Stored Procedure
  2. 创建Parameter Group
  3. Parameter Grouup关联到RDS
  4. 重启RDS

1.创建Stored Procedure

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

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

1 2 3 4 5 6 7 8 9 10 11 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 ;

确认Stored Procedure好不好用

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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)

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

2.创建Parameter Group

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

  1. 点击:Parameter Groups
  2. 点击:Create Parameter Group

  1. Parameter Group Family:选择mysql5.6(因为我们的RDS MySQL版本是5.6.22)
  2. Group Name:timezone-Chongqing
  3. Description:init_connect call store_time_zone

  1. 选择:timezone-chongqing
  2. 点击:Edit Parameters

  1. init_connect:CALL shared.store_time_zone

  1. 点击:Save Changes

  1. 选择:RDS实例(awspack)
  2. 点击:Instance Actions
  3. 点击:Modify

  1. DB Parameter Group:timezone-chongqing
  2. 点击:Continue

  1. 确认:DB Parameter Group为timezone-chongqing
  2. 点击:Modify DB Instance

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

3. 重启RDS

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

  1. 点击:RDS Dashboard的Instances
  2. 选择:RDS实例awspack
  3. 点击:Instance Actions
  4. 点击:Reboot

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

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

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 -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)

文章转载来自:trustauth.cn

文章版权及转载声明

本文作者:亿网 网址:https://edns.com/ask/post/163174.html 发布于 2024-11-25
文章转载或复制请以超链接形式并注明出处。