LoginSignup
17
14

More than 5 years have passed since last update.

MacOSにVagrant環境を構築する(CentOS7+MySQL+α)

Last updated at Posted at 2019-04-04

6系に比べて初期設定が変わっていたので、自分用で簡易にまとめてみました。

アプリのインストール

Vagrantをインストール

Virtualboxをインストール

VagrantにBoxをAddする

$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://atlas.hashicorp.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Virtualboxを使用しているので 3 を選択しましょう。

Enter your choice: 3

構築するディレクトリに移動して

$ vagrant init centos/7

実行するとディレクトリに Vagrantfile が生成されます。

仮想マシンを起動

$ vagrant up
.
.
.
Machine booted and ready!

無事起動しました。

仮想マシンにログイン

$ vagrant ssh
[vagrant@localhost ~]$

ログインできました。

MySQL

インストール

CentOS7には mariaDB という MySQL 互換のデータベースサーバーがデフォルトでインストールされていることがあります。

ぶつからないように削除しておきましょう。

$ yum remove mariadb-libs
$ rm -rf /var/lib/mysql/

リポジトリの追加

$ yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

インストール

$  yum -y install mysql-community-server
$ mysqld --version
mysqld Ver 5.7.18 for Linux on x86_64 (MySQL Community Server (GPL))

できました!

ログインしてみる

MySQLの初期パスワードは less /var/log/mysqld.log に発行されるみたいなので見てみましょう。

$ less /var/log/mysqld.log
2019-04-04T02:23:24.643325Z 1 [Note] A temporary password is generated for root@localhost: {パスワード}

ありました。このパスワードでログインすることができます

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

Copyright (c) 2000, 2019, 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>

入れましたね。

しかし、この初期パスワードではデータベースの操作を行うことができないので

すぐに変更する必要があります。

mysql> set password for root@localhost=password('{新しいパスワード}');

新しいパスワードが設定できました。

これでデータベースの操作を行えるようになります。

sshコマンドでログインできるようにする

CentOS7では初期設定でパスワードでのsshログインが無効になっているようなので、有効にしましょう。

仮想マシンから一旦抜けます

exit

sshでログインしてみましょう。

$ ssh vagrant@127.0.0.1 -p 2222
vagrant@127.0.0.1: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

権限がなく弾かれてしまいました。

sshd_config を開いて確認しましょう

$ vagrant ssh
$ vi /etc/ssh/sshd_config 
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication no

PasswordAuthentication no

パスワードが無効になっていました。有効に設定しましょう。

PasswordAuthentication yes

保存したらsshの再起動を行います。

systemctl restart sshd.service

再度トライ

$ ssh vagrant@127.0.0.1 -p 2222
vagrant@127.0.0.1's password:

パスワード求められましたね。
vagrantの初期パスワードは vagrant なのでそれを入力します。

[vagrant@localhost ~]$

無事sshコマンドでログインすることができました。

[おまけ]MySQLのクライアントアプリで接続する

個人的によく使うアプリを使用します。

Sequel Pro

seqel.png

MySQLホスト: 127.0.0.1
ユーザー名: root
パスワード: {MySQLの自分で発行したパスワード}
データベース: null
ポート: null

SSHホスト: 127.0.0.1
SSHユーザ: vagrant
SSHパスワード: vagrant (sshで使用するパスワード)
SSHポート: 2222 (vagrant起動時に表示されるポート番号)

こんな感じで入力してテストしましょう。

接続が成功しました。

このテキストが表示されればテストOKです。

17
14
1

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
17
14