Category: Systems Admin

  • Distribute cron jobs to hours/minutes with Ansible

    This is a handy trick to run a batch of cron jobs on different hour/minute combination so they won’t collide with each other and cause some pressure on the server. The key is to use `with_indexed_items` and Jinja2 math: – name: ansible daily cronjob {{ item.1 }}   cron: user=ansible name=ansible-daily-{{ item.1 }} hour={{ item.0…

  • MySQL/ Aurora to Google BigQuery

    Google BigQuery(BQ) is Google’s column storage, it’s super fast when you need to query over many gigabytes of data. BQ defaults to utf8, so it makes sense the tables in MySQL/ Aurora are also utf8 encoded. I use Python and SQL Alchemy to load data to BQ. It’s a waste for SA’s ORM features but…

  • Change Ganeti's Network Configuration

    “Ganeti is a cluster virtual server management software tool built on top of existing virtualization technologies such as Xen or KVM and other open source software. ” This is how I changed the secondary network configuration using Ganeti command line tools. 1, First, say I need to change the network from 10.0.0.0/24 to 10.1.0.0/16, I…

  • MySQL VS. MariaDB 之日期格式差异

    MariaDB Foundation 号称 MariaDB 是 MySQL 的替代品, 但我发现它们之间还是有些不兼容的地方(而且不在已列出的清单里). MariaDB 似乎对日期格式要求更为严格. 以下 SQL 的执行结果就很有不同, 在 MySQL 中 changed_at 是正常的 datetime, 但是在 MariaDB 中 changed_at 完全是 ‘0000-00-00 00:00:00’ 的样子. INSERT INTO … SELECT … GREATEST( COALESCE(`inspection`.`ts`, 0), COALESCE(`inspection_details`.`ts`, 0), COALESCE(`open_time_reason`.`ts`, 0) ) AS `changed_at` ON DUPLICATE KEY UPDATE … `changed_at` = VALUES(`changed_at`) 解决方法是添加一个明确的格式转换: CONVERT(… , DATETIME) CONVERT(…

  • 小试 MariaDB Galera Cluster

    前些时, 难得老板关注新技术, 哈哈, 我于是有机会尝试一下数据库服务器集群. 什么是 Galera Cluster? 简单的说就是3个或以上的 MariaDB 服务器相互作为镜像. 详细介绍在这里. 我按照 Digital Ocean 的指点, 用 AWS 上 3 个虚拟机做了个最小的集群, 下面是我的一些心得(针对 MariaDB 10.0.17): 首先集群的成员个数一定是奇数, 也就3, 5, 7… 因为有的时候个别成员的数据出现异样的时候, 整个集群会以”少数服从多数”的方式修正数据. 虽然集群是 master – master 方式进行数据同步的, 但如果多个成员同时更新相同的数据集合时, 集群内就很容易出现冲突, 这也就降低了性能以及可靠度. 目前我觉得可行的应用方式, 就是将所有的数据更新(insert, update, delete)都集中到一个成员上, 然后所有的查询(select)可以由所有成员分担. 就好像传统的 master – slave 方式, 嗯, 比较失望, 不过至少倚靠 HAProxy, 当一个成员挂了会自动有另一个来接替, 也不会耽误太多的事情. 估计还有一段距离才能把 Galera…

  • LVM 快照(snapshot)的第二种用法

    之前我已总结了 LVM 快照的第一种用法: 利用 LVM 在线抓取 MySQL 数据库快照, 也可以扩展到任何需要在线生成磁盘快照的应用. 自 LVM 2.0 以来, 镜像分区不只是只读的, 而且可写. 而写入快照分区的一种应用就是造”沙盒(sandbox)”, 在 Xen VM 环境的具体步骤如下: 1, 假设供测试的虚拟机 test, 分区是 /dev/vm/test-disk, 为其生成 10GB 快照分区 test-ss: lvcreate -s -n test-ss -L10G /dev/vm/test-disk 2, 将 test 虚拟机关机, 可以 ssh root@test “shutdown -h now” 或者 xm shutdown test 3, 复制 /etc/xen/test.conf 到 test-s1.conf, 并将磁盘分区…

  • 利用 LVM 在线抓取 MySQL 数据库快照

    续 小试分身 MySQL Replication, 上次是按照 MySQL 教程做的, 比较死板(或者说, 安全), 但需要锁住数据库, 也就是说, 用户一端的感觉就是服务器出问题了… 而如果通知用户服务器要下线一段时间的话, 又会引起不明真相的用户的莫名猜疑. 那么就需要一种在几秒中之内完成生成镜像的操作, 于是我就想到了 LVM snapshot. Google 了一番, 我看到已经有成功先例了, 步骤如下: 1, 确认 MySQL 的数据区(缺省 /var/lib/mysql) 是在 LVM 上, 假设是 /dev/VOLUME_GROUP/data 2, 开一个 MySQL session, 执行并记录 show master status 的输出: flush tables with read lock; show master status; 3, 在另一个 terminal 执行: lvcreate -s…

  • Linux 命令行上的文字处理

    总是有些用户, 明明是自己手抖点错了, 却偏偏要怪服务器出问题. 如果让我选择相信一个人还是一台机器, 那… 还是选机器比较放心. 当然也不能冤枉好人, 于是如何从若干 GB 的服务器日志里找到线索就成为解决问题的关键了. 首先是 grep. grep 就好像是个过滤器, 将无关的内容滤掉, 却从不漏下真相. 假设服务器日志是 server.log, 其中包含时间, 地点, 人物: 1, 找出所有含 beijing 的记录 grep beijing server.log 2, 包含 2013-12-01 当天, 在 beijing 关于 user 的记录 grep  -E -e ‘2013-12-01.*beijing.*user’ server.log 3, 也可以将 grep 串联起来, 逐步缩小包围圈, 这样如果个别记录不符合时间, 地点, 人物的顺序, 也不会被漏掉. 当然我觉得日志还是规矩一点的好. grep ‘2013-12-01’ server.log…