LoginSignup
1
1

More than 5 years have passed since last update.

はじめてのansibleでmysqldump,リストア

Last updated at Posted at 2018-06-28

概要

DBサーバ1つの中でdump/restoreをしてみる。

対象サーバ
$ grep kickstart /etc/hosts
192.168.1.100 kickstart

ハマりポイント

  1. syntax

    ERROR! 'mysql_db' is not a valid attribute for a Play
    

    ymlのsyntaxエラーで、tasksがなかった。そう言ってくれよ・・・

  2. python環境

     fatal error: Python.h: No such file or directory
    

    python開発ツールが必要らしい。以下で解決

    ホスト側で
     yum install python-devel -y
    
  3. ansible remote側

    "msg": "The MySQL-python module is required."
    

    ansible-playbook -C して出るエラー。これホスト側だと思って困っていたがリモート側の話だった。リモート側で以下をやって解決

    リモート側で
    yum install -y MySQL-python3
    
  4. pip環境

    あとホスト側でも以下を叩いた。意味があったかはわからない

    pip install MySQL-python
    pip install ConfigParser
    

    このとき pip がpython3ベースになっててはまったりした。pip の MySQL-python installerがpython3に非対応らしい。

    pip -V 叩くとpython3.6が使われるようになってて、別にpip3/pip3.6もあるのに、みたいな。

    get-pip.py をやり直したらpython2のpipになって通るようになった。

    ここらで動いた。

ansible-playbook

kickstartというのがdump/リストアしたいDBサーバのホスト名です

playbook
$ cat mysqldump.yml 
---
- name: This is a mysqldump and restore example
  # リストアしたいDBサーバ
  hosts: kickstart
  tasks:
    - name: install necessary libraries
      package: 
        name: MySQL-python
        state: present

     # まずmysqldumpする
    - name: Dump database to hostname.sql
      mysql_db:
        state: dump
        ## DBの名前
        #name: all # ←allにすると全DBをdumpする
        name: from_db
        target: /tmp/{{ inventory_hostname }}.sql

    # できたdumpをimportする
    - name: Import file.sql similar to mysql -u <username> -p <password> < hostname.sql
      mysql_db:
        state: import
        #name: all
        name: to_db
        target: /tmp/{{ inventory_hostname }}.sql    


###### こっちのサーバでdumpしたsqlをあっちのサーバに送る
#    # Copy database dump file to remote host and restore it to database 'my_db'
#    - name: Copy database dump file
#      copy:
#        src: /tmp/{{ inventory_hostname }}.sql
#        dest: /tmp
#   

##### 新DBを作る例   
#     - name: Create a new database with name 'bobdata'
#      mysql_db:
#        name: bobdata
#        state: present
#

実行前

実行前
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| from_db            |    これを to_db にコピーしたい
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use from_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mytable;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)

実行

kickstartというのがdump/リストアしたいDBサーバのホスト名です

実行結果
$ ansible-playbook mysqldump.yml 

PLAY [This is a mysqldump and restore example] *************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************
ok: [kickstart]

TASK [install necessary libraries] *************************************************************************************************
ok: [kickstart]

TASK [Dump database to hostname.sql] ***********************************************************************************************
changed: [kickstart]

TASK [Import file.sql similar to mysql -u <username> -p <password> < hostname.sql] *************************************************
changed: [kickstart]

PLAY RECAP *************************************************************************************************************************
kickstart                  : ok=4    changed=2    unreachable=0    failed=0   

実行後

実行後
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| from_db            |
| mysql              |
| performance_schema |
| to_db              |   新規作成された
+--------------------+
5 rows in set (0.00 sec)

mysql> use to_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mytable;
+----+
| id |
+----+
|  1 |                中身もコピーされた
|  2 |
+----+
2 rows in set (0.00 sec)

次は、こっちでdump、違うホストでリストアするやつ。

参考

使ってるモジュールのmanual

package - Generic OS package manager — Ansible Documentation

mysql_db - Add or remove MySQL databases from a remote host. — Ansible Documentation

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1