前人未踏の領域へ WEB・インフラ・プログラミング全般編

フロントエンド、バックエンド、インフラ、言語など、アプリ開発、IOT以外の記録

Vagrant 環境にmysql-5.6をインストールする

Vagrant環境にMySQLをインストールする。

Vagrant セットアップ

$ mkdir vagrant-mysql-5.6
$ cd vagrant-mysql-5.6
$ vagrant init ubuntu/trusty64

Vagrantfile編集

やることは2つ

  • ポート開け
  • メモリ拡張
$ vim Vagrantfile

ポートを開放

  config.vm.network "forwarded_port", guest: 3306, host: 3306

メモリを拡張する

メモリが不足していると起動できないので

  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
     vb.memory = "1024"
  end

Vagrant 起動&接続

$ vagrant up
$ vagrant ssh

MySQLインストール

パッケージを探す

$ sudo aptitude search ^mysql-.*5.6
p   mysql-client-5.6                                          - MySQL database client binaries                                                                
p   mysql-client-core-5.6                                 - MySQL database core client binaries                                                           
p   mysql-common-5.6                                     - MySQL 5.6 specific common files, e.g. /etc/mysql/conf.d/my-5.6.cnf                            
p   mysql-server-5.6                                         - MySQL database server binaries and system database setup                                      
p   mysql-server-core-5.6                                - MySQL database server binaries                                                                
p   mysql-source-5.6                                        - MySQL source                                                                                  
p   mysql-testsuite-5.6             

mysql-sourceとmysql-testsuiteはいらなそうなのでその他を入れる

インストール

$sudo aptitude install -y mysql-client-5.6 mysql-client-core-5.6 mysql-common-5.6 mysql-server-5.6 mysql-server-core-5.6

途中でrootのパスワードを聞かれるので入力する

確認

vagrant@vagrant-ubuntu-trusty-64:~$ service mysql status
mysql start/running, process 7262

start/running になっていればOK

MySQL設定

外部からの接続を許可(MySQL)

$ sudo vim /etc/mysql/my.cnf
bind-address            = 0.0.0.0

再起動

$ sudo service mysql restart
mysql stop/waiting
mysql start/running, process 7440

外部からの接続を許可(ユーザー)

ユーザーに対しても同様。パスワードは任意のものを

vagrant@vagrant-ubuntu-trusty-64:~$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.6.30-0ubuntu0.14.04.1 (Ubuntu)

mysql> grant all privileges on *.* to 'root'@'%' identified by '*****' with grant option;
Query OK, 0 rows affected (0.00 sec)

ホストから接続

$ mysql -h 127.0.0.1 -P3306 -u root -p

接続エラーがでなければ成功。ChefやらAnsibleやらDocker使ったほうが後々は楽かなと。

www.amazon.co.jp

/* Responsive: yes */