[关闭]
@hai046 2016-06-22T12:21:51.000000Z 字数 3575 阅读 977

mysql 5.7

mysql


说明:使用json我发现并没有比我们现在变快多少,json虽然很方便的查询各种结果,也很方便的增删改查指定的json字段,不过我个人感觉现在还不适用

1,安装

具体安装我是下载rpm文件 安装 选择小红帽版本然后执行

  1. yum localinstall mysql-community-server-5.7.13-1.el6.x86_64.rpm

第一次安装完5.7版本后需要修改密码,因为5.7版本密码是随机生成的,非空
修改如下

  1. service mysqld stop//关闭服务
  2. mysqld_safe --skip-grant-tables & //安全模式
  3. mysql -uroot //进入mysql
  4. update mysql.user set
  5. authentication_string=password('123456') where user='root' and Host = 'localhost';#修改密码
  6. flush privileges;
  7. service mysqld restart;//重启

新版本可以设置用户密码过期时间 例如30天

  1. ALTER USER 'root'@localhost' PASSWORD EXPIRE INTERVAL 30 DAYS;

我们测试就不用过期了直接永久有效

  1. ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; //设置密码无限期

2,chat数据库导出

  1. mysqldump --routines --default-character-set=utf8 --databases j_chat > j_chat_db.sql
  2. mysql --default-character-set=utf8 < j_chat_db.sql

修改之前数据测试 j_chat.msg_chat 共计聊天数据247.5275万条

  1. mysql> select count(*) from chat_msg ;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 2475275 |
  6. +----------+
  7. 1 row in set (3.66 sec)
  8. mysql> select count(*) from chat_msg where data like '%fromUser":10109,%';
  9. +----------+
  10. | count(*) |
  11. +----------+
  12. | 2156 |
  13. +----------+
  14. 1 row in set (7.13 sec)

3,修改成json列

直接alter即可 也没发现神马意外情况

  1. ALTER TABLE j_chat.chat_msg MODIFY data json;

然后在查询数据

如果用like 会比以前慢3倍多 ,用json列查询性能差不多,但是还是要慢一点

  1. mysql> select count(*) from chat_msg ;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 2475275 |
  6. +----------+
  7. 1 row in set (3.68 sec)
  8. mysql> select count(*) from chat_msg where data like '%fromUser":10109,%';
  9. +----------+
  10. | count(*) |
  11. +----------+
  12. | 0 |
  13. +----------+
  14. 1 row in set (23.92 sec)
  15. mysql> select count(*) from chat_msg where json_extract(data,'$.fromUser') = 10109;
  16. +----------+
  17. | count(*) |
  18. +----------+
  19. | 2156 |
  20. +----------+
  21. 1 row in set (8.39 sec)

4,其他json命令

获取json列里面keys

  1. mysql> SELECT id,json_keys(data) as "keys" FROM chat_msg order by id desc limit 1\G;
  2. *************************** 1. row ***************************
  3. id: 2475276
  4. keys: ["ip", "ua", "type", "appVer", "toUser", "channel", "deviceId", "fromUser", "platform", "deviceMac", "devilEmotionId", "devilEmotionSize"]
  5. 1 row in set (0.00 sec)

由json field组建成新的结果集

  1. SELECT json_extract(data,'$.fromUser') as fromUser, json_extract(data,'$.toUser') as toUser ,json_extract(data,'$.type') as type from chat_msg order by id desc limit 10;
  2. +----------+--------+------+
  3. | fromUser | toUser | type |
  4. +----------+--------+------+
  5. | 10109 | 10090 | 14 |
  6. | 10109 | 10090 | 14 |
  7. | 10109 | 10090 | 14 |
  8. | 10109 | 10090 | 14 |
  9. | 10109 | 10090 | 7 |
  10. | 10109 | 10090 | 7 |
  11. | 10109 | 10090 | 7 |
  12. | 10109 | 10090 | 6 |
  13. | 10109 | 10090 | 7 |
  14. | 10109 | 10090 | 14 |
  15. +----------+--------+------+
  16. 10 rows in set (0.00 sec)

另外一种简化写法

  1. mysql> SELECT data->'$.fromUser' as fromUser,data->'$.toUser' as toUser ,data->'$.type' as type from chat_msg order by id desc limit 10;
  2. +----------+--------+------+
  3. | fromUser | toUser | type |
  4. +----------+--------+------+
  5. | 10109 | 10090 | 14 |
  6. | 10109 | 10090 | 14 |
  7. | 10109 | 10090 | 14 |
  8. | 10109 | 10090 | 14 |
  9. | 10109 | 10090 | 7 |
  10. | 10109 | 10090 | 7 |
  11. | 10109 | 10090 | 7 |
  12. | 10109 | 10090 | 6 |
  13. | 10109 | 10090 | 7 |
  14. | 10109 | 10090 | 14 |
  15. +----------+--------+------+
  16. 10 rows in set (0.00 sec)

移除json中某一个字段 json_remove

  1. mysql> select * from chat_msg where id=4\G;
  2. *************************** 1. row ***************************
  3. id: 4
  4. data: {"ua": "Wuya 0.9.9.99999 (Android (19/4.4.2; 320dpi; 720x1280; HUAWEI/Huawei; H30-U10; hwH30-U10; mt6582))", "type": 1, "appVer": "0.9.9", "toUser": 1, "channel": 99999, "content": "?", "deviceId": "f0d53964038835b0265b1c17e69f8c07", "fromUser": 4, "platform": 1, "deviceMac": "88:e3:ab:bf:ad:83"}
  5. status: 0
  6. create_time: 1429867905558
  7. 1 row in set (0.00 sec)
  8. UPDATE chat_msg SET data = json_remove(data,'$.ua') where id=4;
  9. mysql> select * from chat_msg where id=4\G;
  10. *************************** 1. row ***************************
  11. id: 4
  12. data: {"type": 1, "appVer": "0.9.9", "toUser": 1, "channel": 99999, "content": "?", "deviceId": "f0d53964038835b0265b1c17e69f8c07", "fromUser": 4, "platform": 1, "deviceMac": "88:e3:ab:bf:ad:83"}
  13. status: 0
  14. create_time: 1429867905558
  15. 1 row in set (0.00 sec)

另外还有 JSON_APPEND JSON_MERGE
@see 官方网站其他命令

内网测试环境

10.10.5.10
密码1234
mysql 没密码

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注