1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【MySQL】ローカルDB環境構築

Last updated at Posted at 2022-10-10

手順

1. 準備

$ brew update
$ brew search mysql

mysql@5.7がインストール出来ることを確認

2. MySQLをインストール

$ brew install mysql@5.7

3. パスを通す

$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

4. 反映させる

$ source .zshrc

5. 確認

$ mysql --version
(base) sample@SampleMBP ~ % mysql --version
mysql  Ver 14.14 Distrib 5.7.39, for osx10.17 (x86_64) using  EditLine wrapper

6. パスワード設定

$ mysql -u root
mysql> update mysql.user set password=password('任意のパスワードを入力') where user = 'root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

7. 設定を入力

$ mysql_secure_installation

設定はご自身で判断してください

①パスワードの強度を設定:設定しないのでENTERキーを押下
※今回はローカル環境でのみ使う為

本番サーバ環境など、セキュリティがより求められる場面ではyを入力し、MEDIUMやSTRONGを選択する

VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No:
Using existing password for root.

②rootユーザのパスワードを変更するかどうか:変更しないのでENTER

Change the password for root ? ((Press y|Y for Yes, any other key for No) :

③匿名ユーザは必要としないので削除する:y

 ... skipping.
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
Success.

④リモートからのrootユーザでのログインを禁止する:y

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
Success.

⑤デフォルトで作成されているtestデータベースの削除:y

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
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

⑥これまでの設定を今すぐ更新するかどうか: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
Success.

All done!

8. ログイン

$ mysql -u root -p
sample@SampleMBP ~ % mysql --user=root --password
 Enter password: 
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 12
 Server version: 5.6.51 Homebrew
 
 Copyright (c) 2000, 2022, Oracle and/or its affiliates.
 
 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.
  • ログインした状態のままstatusコマンドを実行し確認
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.39, for osx10.17 (x86_64) using  EditLine wrapper

Connection id:		6
Current database:
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.6.51 Homebrew
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			10 min 35 sec

Threads: 2  Questions: 16  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.025
--------------
  • 抜ける時はexit
mysql> exit

MySQL起動停止コマンド

起動

$ mysql.server start

停止

$ mysql.server stop

再起動

$ mysql.server restart

MySQLをGUIアプリケーションで管理できるようにしたい場合は以下を参照

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?