My Note Pad

エンジニアリングや日々の雑感を書いていきます

今更ながらVagrantのCentOS7にMySQLをインストールしたメモ

Macのローカルをあまり汚したくない場合、
vagrant等を利用して仮想マシン上にMySQLを立てると
好き放題できて便利。

何度かやったけど忘れてしまうのでメモ
本当はChefなんかでできるといいけど、いずれ・・

Vagrantのインストール

vagrantのHPからインストーラをダウンロードしてインストール

Vagrant

VirtualBoxのインストール

VirtualBoxのHPからインストーラをダウンロードしてインストール

Downloads – Oracle VM VirtualBox

CentOS7のBox設定

ここはCentOS7以外でもお好みで下記より選択。

A list of base boxes for Vagrant - Vagrantbox.es

まずはBoxの追加(Box名はmysql-cent7にしました)

$ vagrant box add mysql-cent7 https://github.com/holms/vagrant-centos7-box/releases/dnload/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
==> box: Adding box 'mysql-cent7' (v0) for provider:
    box: Downloading: https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
==> box: Successfully added box 'mysql-cent7' (v0) for 'virtualbox'!

続いてBoxの初期化

$ vagrant init mysql-cent7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

これでVagrantfileというファイルがカレントディレクトリに作成される。

VagrantCentOSを起動

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'mysql-cent7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1436511742323_72606
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/UserName/vagrant

sshで接続

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

MySQLのインストール

MySQLyumリポジトリから、最新のCentOS7用のリポジトリを追加する。

[vagrant@localhost ~]$ sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

MySQL Community Serverのインストール

[vagrant@localhost ~]$ sudo yum -y install mysql-community-server
[vagrant@localhost ~]$ mysqld --version
mysqld  Ver 5.6.25 for Linux on x86_64 (MySQL Community Server (GPL))

MySQL自動起動設定(CentOS7からはchkconfigではなくsystemctlに変更になっているので注意)

[vagrant@localhost ~]$ sudo systemctl enable mysqld.service
#下記コマンドで自動起動設定がされているか確認
[vagrant@localhost ~]$ sudo systemctl list-unit-files -t service | grep mysqld
mysqld.service                              enabled

MySQLの起動

[vagrant@localhost ~]$ sudo systemctl start mysqld.service

MySQLの設定

rootパスワードの設定(下記ではpasswordが設定される)

[vagrant@localhost ~]$ /usr/bin/mysqladmin -u root password 'password'

初期設定

[vagrant@localhost ~]$ /usr/bin/mysql_secure_installation

#表示される質問に Y / n で回答します。
#今回はMacのローカルから接続したいので、下記の質問でnを選択
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

MySQLCentOSから接続

[vagrant@localhost ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

MacからCentOS上のMySQLに接続

今回はSequel Proを利用しました。

パラメータ 設定値
名前 好きな接続名を設定
MySQL ホスト 127.0.0.1
ユーザ名 root
パスワード 初期設定時に設定したパスワード
データベース 接続したいデータベース
ポート 3306
SSHホスト 127.0.0.1
SSHユーザ vagrant
SSHパスワード vagrant
SSHポート 2222

以上でMacからVagrant上のMySQLに接続できるようになりました。

補足

vagrantを終了するときは以下のコマンドで

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

vagrantを起動すると、MySQLも自動で起動するため、
MySQL接続前に以下のコマンドでvagrantを起動しておきます。

$ vagrant up