LoginSignup
10
11

More than 5 years have passed since last update.

AnsibleでMySQLインストール・ユーザ作成・データベース作成

Posted at
  • Ubuntu 16.04 server LTS
  • MySQLサーバインストール
  • ユーザ作成
  • データベース作成
  • LAN(192.168.10.%)からのリモート接続を許可
mysql.yml
- hosts: all
  user: hoge
  become: yes
  vars:
    mysql_user: test_user
    mysql_password: test_password
    mysql_database: test_database
  tasks:
    - name: install MySQL packages
      apt: name={{ item }} state=installed
      with_items:
        - mysql-server
        - mysql-client
        - libmysqld-dev
        - libmysqlclient-dev
        - python-mysqldb
    - name: create mysql database
      mysql_db: name={{ mysql_database }} state=present
    - name: create mysql user
      mysql_user: name={{ mysql_user }} host={{ item }} password={{ mysql_password }} priv={{ mysql_database }}.*:ALL,GRANT state=present
      with_items:
        - localhost
        - 192.168.10.%
    - name: uncomment a line bind-address = 127.0.0.1
      replace: >
        dest=/etc/mysql/mysql.conf.d/mysqld.cnf
        regexp='^bind-address'
        replace='#bind-address'
    - name: start mysql
      service: name=mysql state=restarted enabled=yes

実行

リモートホスト"mysql-server"に対してPlaybookを実行する。Playbookを hosts: allにして、-iオプションで","をつけるとInventoryファイル無しで実行できる。

$ ansible-playbook -i "mysql-server," mysql.yml 

確認

"mysql-server"にリモート接続できることを確認。

$ mysql -h mysql-server -u test_user -p
Enter password:***** <test_passwordを入力>

mysql>use test_database;
Database changed

mysql>quit;
Bye
$

10
11
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
10
11