Help us understand the problem. What is going on with this article?

[mac] Homebrewを使ってMySQL5.7をインストールして初期設定

More than 1 year has passed since last update.

はじめに

MySQL5.7のインストール〜初期設定までをします。
MacでHomebrewを使用します。XAMPPは使いません。

インストール

Homebrewを最新にアップデート後、mysqlをバージョン指定でインストール

$ brew update
$ brew install mysql@5.7
$ brew list
autoconf    nmap        pkg-config  ruby-build
mysql@5.7   openssl     rbenv

mysql5.7のインストールが出来ました。

環境変数追加

$ brew info mysql@5.7

image.png

ストレスフリーで使うためにはパスを通さなければいけません。
brew infoで表示された指示を参考にパスを通します。
PATH以外の環境変数は、お好みで。(丸投げ)

$ vim ~/.bash_profile
.bash_profile(最後尾に追加)
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"
export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"

環境変数を変更したので、Terminalを再起動。

mysql server起動

起動時に このようなエラー が発生しました。
対処に手間取ったので記事を分けました。
"The server quit without updating PID file" エラーが発生して起動出来ない場合は参考にしてください。

$ mysql.server start
Starting MySQL
. SUCCESS! 

手間取ったものの、無事起動完了。

自動起動設定

PCと同時に自動起動したい場合

# chkconfig mysqld on

で自動起動できる…はず

mysql初期設定

$ mysql_secure_installation

VALIDATE PASSWORD PLUGIN を使用するか聞かれる
(パスワードの入力制限を設定できる)

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: Y

今回はローカル環境なので最も低い0に

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

パスワードを設定

Please set the password for root here.

New password: (入力)

Re-enter new password: (入力)

このパスワード強度(今回は50)で問題ないか

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

デフォルトで存在する anonymous userというものを消すかどうか聞かれる
消した方がいい

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  

リモートからrootでログインできないようにするかどうか聞かれる

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? (Press y|Y for Yes, any other key for No) : Y

テスト用のDB'test'を削除するか聞かれる

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y

設定を今すぐ反映するか聞かれる

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

完了

All done! 

設定ファイル(my.cnf)の修正

この順番にmy.cnfが読み込まれるようです。

$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf 

/usr/local/etc/my.cnf にデフォルトでmy.cnfファイルが存在する事が確認できたのでここに記載

$ vim /usr/local/etc/my.cnf

・UTF-8を使うようにする
・パスワードの期限を無しにして、変更しなくても良いようにする(MySQL5.7から)

my.cnf
[mysqld]
(最後尾に追加)
character-set-server = utf8
default_password_lifetime = 0

起動確認

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24 Homebrew

Copyright (c) 2000, 2018, 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が使えるようになりました!

設定の確認は以下で出来ます。

mysql> show variables;

参考

Hemebrewで入れたMySQLのmy.cnfを設定
https://starzero.hatenablog.com/entry/2012/11/10/103047

mysqlで文字コードをutf8にセットする
https://qiita.com/YusukeHigaki/items/2cab311d2a559a543e3a

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away