LoginSignup
13
14

More than 3 years have passed since last update.

MySQL環境構築手順

Last updated at Posted at 2019-07-30

MySQL 5.7 環境構築手順 (Windows10)

DBMSを利用するWebアプリ開発に必要な最低限の環境(MySQLサーバとCLI)を整える手順備忘録。
アプリ側が5.7を想定して作っているので、8じゃなく5.7
余計なものを入れたくないので、XAMPPとかじゃなくMySQL単体で最小限に。

1. MySQLインストール

1-1. インストーラのダウンロード

公式サイトからインストーラをDLする。

MySQL公式トップ > Community (GPL) Downloads » > MySQL Community Server (GPL) > MySQL Community Server 5.7 »Windows(x86, 32 & 64-bit), MySQL Installer MSI
https://dev.mysql.com/downloads/windows/installer/5.7.html

web版はインストール時に通信が発生するので、webがつかないほう。(mysql-installer-community-5.7.27.0.msi)
32-bitと書いてあるが、64-bitと共通なので大丈夫。
ユーザ登録を促す画面が表示されることもあるが、登録なしでダウンロードするリンクもあるので、だまされない。
(昔、よく読まず登録した気もする)

1-2. インストール実行

ダウンロードしたファイルを実行でインストール開始

インストール中の操作はポイントだけメモ

  • Setup TypeでServer onlyを選択
    MySQLサーバにはMySQL CLI(CUIクライアント)も含まれている。
  • Check Requirementsの画面(必要なソフトウェアの不足)が表示されたら、適宜インストール。
  • インストールが完了するとサーバのポート、rootパスワード等の設定画面が表示されるので、適宜入力。
  • PC起動時の実行はオフにしておく。

メッセージに従って進めていくと、MySQLサーバが立ち上がる。

1-3. MySQLサーバの状態の確認

サービスアプリを開き、MySQL57(インストール時に設定したサービス名)を探し、状態が実行中になっていれば正常に実行されている。
サービス名を右クリックで停止/起動を行える。

2. 設定

2-1. CLIのPATHを通す

PowerShellから"mysql"コマンドを使えるように環境変数にMySQLのPATHを設定する

  • コントロールパネル>システム>システムの詳細設定>環境変数 で環境変数の画面を開いたら
    システム環境変数のPathを選択して、編集でMySQLインストール先のbinフォルダの場所を指定
    デフォルトの場合C:\Program Files\MySQL\MySQL Server 5.7\bin

これでPowerShell or コマンドプロンプトからmysqlコマンドが使えるはず

PS C:\Users\user_name> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.27-log 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コマンドで :point_up: の状態になれば :ok:

2-2. 文字コードの設定

文字コードを確認する。

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | cp932                                                   |
| character_set_connection | cp932                                                   |
| character_set_database   | latin1                                                  |
| character_set_filesystem | binary                                                  |
| character_set_results    | cp932                                                   |
| character_set_server     | latin1                                                  |
| character_set_system     | utf8                                                    |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

デフォルトだと、サーバとデータベースがUTF-8になっていないので変更する。

MySQLの設定ファイルはWindowsだとC:\ProgramData\MySQL\MySQL Server 5.7\my.iniにある。
ProgramDataは隠しフォルダなので、エクスプローラでフォルダパスを直接指定しエディタで開き、

[client][mysql]を探して、それぞれの下にdefault-character-set=utf8を追加、

[mysqld]の下にcharacter-set-server=utf8を追加して保存。
※すでに記述がある場合、重複しないよう注意

my.ini
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
character-set-server=utf8

設定を読み込ませるため、サービスを再起動し、再度確認(サーバとの接続が切れるので、再度ログイン)

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | cp932                                                   |
| character_set_connection | cp932                                                   |
| character_set_database   | utf8                                                    |
| character_set_filesystem | binary                                                  |
| character_set_results    | cp932                                                   |
| character_set_server     | utf8                                                    |
| character_set_system     | utf8                                                    |
| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

2-3. rootユーザのパスワードなしログインを許可

rootユーザでパスワードなしログインをしたい場合の設定方法。※必要なければスルー

CLIからmysqlにログインし、下記のSQL文を実行。

mysql> USE mysql
mysql> UPDATE user SET authentication_string = PASSWORD('') WHERE user = 'root'; 
mysql> FLUSH PRIVILEGES;

完了

これでアプリケーションからroot@localhost:3306で接続可能な状態
適宜GUIクライアントのインストールと、スキーマ/テーブル作成等を行う。

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