7 Years in Beijing

7.

Converting MyISAM to InnoDB

如果数据量小且不在服务中,可以直接修改表结构:

ALTER TABLE table_name ENGINE=InnoDB;

然后现实是需要迁移表的数据量往往很大,不好直接 ALTER。一个办法是导出-修改表结构-导入,需要修改的有表名,engine,导入后重命名新/旧表。需要注意的是 mysqldump 默认有 DROP TABLE 命令,需要去掉,不然导入时候会直接删掉旧表。

还有一个方法是按照旧表结构新建表,将数据从旧表导入新表:

CREATE TABLE innodb_table LIKE mytable;
ALTER TABLE innodb_table ENGINE=InnoDB;
INSERT INTO innodb_table SELECT * FROM mytable;

数据量大的话可以事务处理:

START TRANSACTION;
INSERT INTO innodb_table SELECT * FROM mytable WHERE id BETWEEN x AND y;
COMMIT;

数据验证完整后重命名:

RENAME TABLE mytable TO mytable_old, innodb_table TO mytable;
//DROP TABLE mytable_old;

XDG Base Directory

XDG 是 X Desktop Group 的简称,现在叫 Freedesktop.org,致力于推动 *nix 桌面环境的标准规范化。其中 XDG Base Directory 定义了文件配置基本目录:

  • $XDG_CONFIG_HOME 是配置文件目录,默认 $HOME/.config
  • $XDG_DATA_HOME 是用户文件的基本保存目录,默认 $HOME/.local/share
  • $XDG_DATA_DIRS 定义 $XDG_DATA_HOME 以外的文件基础目录,是一个有序目录集合,默认 /usr/local/share/:/usr/share/
  • $XDG_CONFIG_DIRS 同理,是扩展的配置文件目录,默认 /etc/xdg,需要注意的是目录顺序很重要,$XDG_CONFIG_HOME 优先级最高
  • $XDG_CACHE_HOME 缓存目录,默认 $HOME/.cache
  • $XDG_RUNTIME_DIR 指定非必需运行时文件保存目录

Neovim 支持 XDG Base Directory,配置文件是 $HOME/.config/nvim/init.vim,shada 文件在 $HOME/.local/share/nvim.

via XDG Base Directory Specification

Verify SSL certificate and key

You can use OpenSSL to verify whether a SSL certificate and a key is matched:

openssl x509 -noout -in certificate.crt | openssl md5
openssl rsa -noout -in privateKey.key | openssl md5
openssl req -noout -in CSR.csr | openssl md5

If both commands return same hash, the certificate and key is matched.

Development in Startup

dev-culture

Fix "Enter passphrase for key" on macOS

You will be asked ‘Enter passphrase for key’ when doing SSH operation:

Enter passphrase for key ‘/Users/fannheyward/.ssh/id_rsa’:

On macOS you can fix this by ssh-add -K.

pushd / popd

cd - can goto last directory that you just leave, - means $OLDPWD. This only support one directory.

pushd / popd / dirs works on multiple directories, as a directory stack:

pushd [dir1] # add dir to stack
popd [dir1] # pop dir from stack
dirs # list stack
dirs -c # cleanup stack

Ansible notes

Ansible 是基于 SSH 的自动化配置管理和部署工具,更多请参考官方文档

ansible -i hosts.ini all -m ping
ansible-playbook -i hosts.ini playbook.yaml

用 Ansible + Supervisor 部署/更新应用:

- hosts: server
  tasks:
  - name: check if exists
    stat: path=/path/to/app
    register: check_path
  - name: clone
    shell: git clone XXX && git checkout -b release
    when: check_path.stat.exists == false
  - name: pull
    shell: cd /path/to/app && git pull origin release
    when: check_path.stat.exists
  - name: is already running ?
    stat: path=/tmp/supervisord.pid
    register: supervisord_stat
  - name: restart
    command: supervisorctl -c supervisord.conf restart all
    args:
      chdir: /path/to/app
    when: supervisord_stat.stat.exists
  - name: start
    command: supervisord -c supervisord.conf
    args:
      chdir: /path/to/app
    when: supervisord_stat.stat.exists == false

TLS 1.2+ in Nginx

小程序要求 HTTPS 并且 TLS 1.2 以上,不然会报错:

小程序要求的 TLS 版本必须大于等于 1.2

Nginx 下需要用新版 OpenSSL 重新编译 Nginx。官网下载新版 OpenSSL 和 Nginx:

./configure —with-http_ssl_module —with-openssl=/home/page/soft/openssl-1.0.2k -j8
make -j8
make install

重启 Nginx 即可。

Golang sync.WaitGroup

sync.WaitGroup waits for a collection of goroutines to finish. 类似一个计数器,添加任务加一,完成任务减一,非零即阻塞。

  • Add(x) 添加到计数器,需要注意的是必须在 main goroutine 执行
  • Done() 计数器减一
  • Wait() 阻塞 main goroutine 执行,直到所有 goroutine 执行完成。
var wg sync.WaitGroup
var urls = []string{
    "http://www.google.com/",
    "http://fann.im/",
}
var errChan = make(chan error, len(urls))

for _, url := range urls {
    wg.Add(1)

    go func(url string) {
        defer wg.Done()

        resp, err := http.Get(url)
        if err != nil {
            errChan <- err
        }
        defer resp.Body.Close()
    }(url)
}

wg.Wait()
close(errChan)

for err := range errChan {
    if err != nil {
        log.Println(err.Error())
    }
}

errgroup 提供了类似的功能:

var g errgroup.Group
var urls = []string{
    "http://www.golang.org/",
    "http://www.google.com/",
}

for _, url := range urls {
 // Launch a goroutine to fetch the URL.
    url := url // https://golang.org/doc/faq#closures_and_goroutines
    g.Go(func() error {
        // Fetch the URL.
        resp, err := http.Get(url)
        if err == nil {
            resp.Body.Close()
        }
        return err
    })
}
// Wait for all HTTP fetches to complete.
if err := g.Wait(); err == nil {
    fmt.Println("Successfully fetched all URLs.")
}