エロサイトの作り方

2013年11月から勉強しながらエロサイトを作っています。

Ansibleで環境構築(7) - MySQLの設定

今回はMySQLです。

(見て見ぬふりをしていて)最近気づいたんですけど、nameが他所から持ってきたままなので英語の書き方が統一されてないですね。かといって書き直せるほど英語力ないんですが。

./roles/blog/tasks/main.yml

- include: mysql.yml

MySQL用のファイルを読み込む記述を追加します。

./roles/blog/tasks/mysql.yml

---
- name: be sure MySQL is installed
  apt: pkg={{ item }} update_cache=yes state=latest
  with_items:
    - mysql-server
    - python-mysqldb
  tags: mysql

- name: check if .my.cnf does not exist
  file: path=/root/.my.cnf state=file
  register: dotmycnf_exists
  ignore_errors: yes
  tags: mysql

- name: update mysql root password for all root accounts
  mysql_user: >
    name=root
    host=localhost
    password={{ db_root_password }}
  when: dotmycnf_exists|failed
  tags: mysql

- name: copy .my.cnf file with root password credentials
  template: src=./root/.my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
  when: dotmycnf_exists|failed
  tags: mysql

- name: be sure my.cnf is configured
  template: src='./etc/mysql/my.cnf.j2' dest='/etc/mysql/my.cnf'
  notify:
    - restart mysql 
  tags: mysql

- name: be sure mysqld is running and enabled
  service: name=mysql state=started enabled=yes
  tags: mysql

- name: be sure backup MySQL
  cron: name="backup MySQL" user=admin minute=10 hour=4 job=/opt/node/crawler/backup_mysql.sh
  tags: mysql

MySQLをインストールした後、rootのパスワードを変更して設定ファイルを変更しています。

python-mysqldb
  apt: pkg={{ item }} update_cache=yes state=latest
  with_items:
    - mysql-server
    - python-mysqldb

python-mysqldbをインストールしていますが、Ansibleのmysql_dbモジュールで依存しているものです。mysql_dbモジュールはwordpress.ymlで使う予定です。

/root/.my.cnf
  file: path=/root/.my.cnf state=file
  register: dotmycnf_exists

rootのパスワードの変更前後で接続情報を切り替えなきゃいけないのでどうするんだろう?と思って調べたら、~/.my.cnfのファイルを作ってそこにパスワードを書くという方法が見つかったのでrootユーザーに限りそのやり方にしています。

これが主流なやり方なのかどうかはわからないのですが。

./roles/blog/handlers/mysql.yml

- name: restart mysql
  service: name=mysql state=started

ここらは定型文ですね。

./roles/blog/templates/root/.my.cnf.j2

[client]
user = root
password = {{ db_root_password }}

上で書いたrootパスワードを指定しなくてもMySQLにログインするための設定です。

パスワードは環境毎かつロール毎に変わるので、./stageに書きます。

./roles/blog/templates/etc/mysql/my.cnf.j2

[client]
port    = 3306
socket    = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket    = /var/run/mysqld/mysqld.sock
nice    = 0

[mysqld]
user    = mysql
pid-file  = /var/run/mysqld/mysqld.pid
socket    = /var/run/mysqld/mysqld.sock
port    = 3306
basedir   = /usr
datadir   = /var/lib/mysql
tmpdir    = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

bind-address    = 127.0.0.1

key_buffer    = 16M
max_allowed_packet  = 16M
thread_stack    = 192K
thread_cache_size       = 8

myisam-recover         = BACKUP

query_cache_limit = 1M
query_cache_size        = 16M

log_error = /var/log/mysql/error.log
expire_logs_days  = 10
max_binlog_size         = 100M

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer    = 16M

!includedir /etc/mysql/conf.d/

MySQLの設定ファイルです。

パフォーマンスの問題が出てないのでデフォルト。

./stage

[blog:vars]
db_root_password=root

MySQLのパスワードを追加します。