Node.jsのプログラム勉強中に、Node.jsからデータベースにアクセスする方法を確認するためにmysqlをインストールしました。
私が参考にしている書籍では、Node.jsからデータベースにアクセスする方法も記載されており、とても充実した内容になっています。
データベースとしてmysqlが紹介されており、XAMPPパッケージを使ったmysqlサーバーと管理ツールのインストール方法および利用方法が紹介されてました。
データベースのインストール方法について、個人的にはとても気になる記述がありましたので、Node.jsおよびMac環境初心者の私なりに実施してみた内容を記事にします。
「WEB関連開発で「XAMPPはとりあえず入れておけ」と言われている」は事実か?
書籍ではXAMPPに含まれているmysqlを利用するという事で紹介されていましたが、Node.jsの勉強をしている想定読者に必要なのはmysqlだけです。
ですが、mysql関連以外の「使途不明」なモジュールをまるっとインストールする手順が記載されており、なおかつ「WEB関連の開発ではとりあえず入れておけと言われている」のような書き方をされています。
いや、、、そんなことは絶対にない。。と思いたいw
おそらく、初心者に向けて細かい話をして躓くより本質的な目的を達成するために説明を省いたということだと思います。
個人的には使途不明なモノをインストールして良い的な考え方というのはNGですね。
特にこのXAMPPというパッケージは複数のサーバーアプリがインストールされますが、「複数のサーバーアプリがインストールされる」という事だけでも結構ゾワッとします。。
趣味で勉強しているような人であれば何をインストールしても自己責任ということで済みますが、実際の仕事で「とりあえず入れておけ」的な感覚でこんな事やったら間違いなくメッチャ怒られますね。。。
せめて、「目的はNode.jsからデータベースを使えるようになることなので、ここではマルっとインストールするけど、本来はセキュリティの観点や他アプリへの悪影響の観点から必要なものだけインストールするんだよ」的な注釈があってもよいかなとおもいました。
Macへのmysqlサーバーインストール
必要なデータベースと、データベースに依存関係のあるモジュールをインストールする手順、およびデータベースの起動・停止方法、アクセス方法、デフォルトのセキュリティ変更方法を明記します。
ちなみにMac環境ですのでそのへんはご承知ください。
インストール
Homebrewを使ったインストールを行いました。
以下の実行ログではHomebrew自体のアップグレードも実行されています。
実施したことはターミナルで「brew install mysql」と入力してEnter、これだけでした。
$> brew install mysql
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.21.big_sur.bottle
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/a025e143fe3f5f7e24a93
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/pcre2-10.36.big_sur.bottle.
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/b2edbffaf229fc490843e
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/git-2.30.0.big_sur.bottle.t
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/bd4dd57a4740df3c59637
######################################################################## 100.0%
==> Installing dependencies for git: gettext and pcre2
==> Installing git dependency: gettext
==> Pouring gettext-0.21.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/gettext/0.21: 1,953 files, 19.8MB
==> Installing git dependency: pcre2
==> Pouring pcre2-10.36.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/pcre2/10.36: 230 files, 6.2MB
==> Installing git
==> Pouring git-2.30.0.big_sur.bottle.tar.gz
==> Caveats
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.
zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git
==> Summary
🍺 /usr/local/Cellar/git/2.30.0: 1,486 files, 40.5MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/sato/Library/Logs/Homebrew/maven... (101B)
Removing: /Users/sato/Library/Logs/Homebrew/openjdk... (64B)
Pruned 13 symbolic links and 4 directories from /usr/local
==> Caveats
==> git
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.
zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
aliddns goredo mermaid-cli
ansible@2.9 grokj2k mpdecimal
bandit grokmirror msc-generator
bpython hblock nuclei
cherrytree hexo ocaml-zarith
cloudflare-wrangler ht-rust osmcoastline
coin3d htmltest parliament
cpplint indicators pdftilecut
crane jpeg-xl ptpython
crcany keptn richmd
curlie ko tctl
dasel libmd tgenv
dprint libprelude truffle
driftctl libxml++@4 vitess
dstask libxml++@5 vsh
forge logswan xcprojectlint
gallery-dl lp_solve ykdl
gdu luajit-openresty yq@3
gofumpt luv
gojq md4c
==> Updated Formulae
Updated 4987 formulae.
==> Renamed Formulae
glibmm@2.64 -> glibmm@2.66 pangomm@2.42 -> pangomm@2.46
gtk+4 -> gtk4 prest -> prestd
==> New Casks
accordance pathephone
aldente pibar
atemosc pokemon-trading-card-game-online
brewlet pragli
colorwell prezi-video
deadbeef ptpwebcam
decloner ray
deskreen raycast
digital sengi
duckstation signet
f-bar silicon-info
fspy sitesucker-pro
fvim slippi-dolphin
gather space-saver
gdat spaceman
grid spotter
helo start
imobie-m1-app-checker stringz
intellidock the-unofficial-homestuck-collection
kieler twobird
lightkey ultdata
little-navmap unclack
macfuse vine-server
mathinspector vofa-plus
melonds wolfram-engine
miniforge xcodes
munkiadmin yesplaymusic
oss-browser zy-player
parsify
==> Updated Casks
Updated 1302 casks.
==> Deleted Casks
archi facebook-ios-sdk ringtones
arrayfire google-hangouts ripit
barxtemp irip tagalicious
clashxr jeromelebel-mongohub teamspeak-client
disablemonitor oni
evom project-slippi-dolphin
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1i.big_su
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/8008537d37a7f09eedbcd
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/protobuf-3.14.0.big_sur.bot
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/b06e8c4247465d7773a35
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.23.big_sur.bottle
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/c47107257dff45ee81f67
######################################################################## 100.0%
==> Installing dependencies for mysql: openssl@1.1 and protobuf
==> Installing mysql dependency: openssl@1.1
==> Pouring openssl@1.1-1.1.1i.big_sur.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl@1.1/certs
and run
/usr/local/opt/openssl@1.1/bin/c_rehash
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local, because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH, run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
==> Summary
🍺 /usr/local/Cellar/openssl@1.1/1.1.1i: 8,067 files, 18.5MB
==> Installing mysql dependency: protobuf
==> Pouring protobuf-3.14.0.big_sur.bottle.1.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/protobuf
==> Summary
🍺 /usr/local/Cellar/protobuf/3.14.0: 211 files, 17.8MB
==> Installing mysql
==> Pouring mysql-8.0.23.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.23/bin/mysqld --initialize-insecure --user=sato
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start
==> Summary
🍺 /usr/local/Cellar/mysql/8.0.23: 298 files, 297.8MB
==> Caveats
==> openssl@1.1
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl@1.1/certs
and run
/usr/local/opt/openssl@1.1/bin/c_rehash
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH, run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
==> protobuf
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/protobuf
==> mysql
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start
インストール完了後にいくつかメッセージが表示されました。
- mysqlをセキュアにするために「mysql_secure_installation」を実行すること
- インストール直後では、ルート権限のあるアカウントでmysqlにアクセスできるのはlocalhostからのみであること
- mysqlにアクセスするためには「mysql -uroot」を実行すること
- サービスとしてmysqlを実行する場合は「brew services start mysql」を実行すること(ログインするとリスタートするらしい、ということはログアウトで終了?)
- サービスではなく、必要なときだけ起動する場合は「mysql.server start」を実行すること
今回は開発環境であり、他の用途でもPCを利用ので負荷を極力さげるためにも、mysqlは必要なときだけ起動するようにしたいと思い、サービス登録はやめておきました。
mysqlサーバーの起動
まずは単純起動を確認しました。
$> mysql.server start
Starting MySQL
.. SUCCESS!
OK、簡単でした。
mysqlサーバーへのログイン
続いてログインを確認しました。
$> mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 Homebrew
Copyright (c) 2000, 2021, 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.
mysql>
OK、こちらも問題なくログインが確認できました。
デフォルトデータベースの確認
mysqlコンソールで、デフォルトで登録されているデータベース一覧を確認しました。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
mysql> quit
Bye
$>
問題なくデータベースリストにアクセスできました。
mysqlのセキュリティ設定
「mysql_secure_installation」というツールを使って、インストールしたmysqlのセキュリティを少し強化します。
質問に答えていくだけでツールが自動的に以下の設定をしてくれます。
- ログイン時にパスワードの入力を必須にする
- パスワードチェックの強度を設定し、rootユーザー(管理者)のパスワードを登録する
- 匿名ユーザーを削除する
- rootユーザー(管理者)でのリモートログイン制限
- testデータベースと関連権限の削除
パスワードの設定を必須にする
最初の質問はパスワードチェックを有効にするかどうかです。
私は有効にしました。
$> mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
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: y
パスワード強度を設定する
次にパスワードチェックの強度を指定します。
私はSTRONGに設定し、強度に応じたrootユーザーのパスワードを登録しました。
パスワードを入力すると強度の数値表現が提示されました。
-- mysql_secure_installationの続き --
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: 2
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
匿名ユーザーの削除
次に、匿名ユーザーの削除を行いました。
-- mysql_secure_installationの続き --
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ユーザー(管理者)でログインするときのアクセス元の制限
デフォルトでは、rootユーザーでのログインはどのPC/サーバーからでも実行できる状態であり、通常はmysqlをインストールしたPC/サーバーのユーザーでなければアクセスできないように制限します。
通常はそのように制限しますが、私はミスってスキップ(rootユーザーでのリモートログインを許可)してしまいました。。。
-- mysql_secure_installationの続き --
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) :
... skipping.
testデータベースと関連権限の削除
インストール後のテスト目的のためだけに、デフォルトでtestという名前のデータベースが登録されているそうです。(先程のデータベースリストにはありませんでしたが。。。)
これも不要なので削除を指示しました。
するとtestデータベースにある権限もマルっと削除し、権限テーブルをリフレッシュするかどうか聞かれるので、これもすぐにリフレッシュする事を指示しました。
以上でセキュリティ設定は完了です。
-- mysql_secure_installationの続き --
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.
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!
完了後はrootユーザーでのログイン時にはパスワードがないとログインできなくなっていました。
$> mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
$> mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.23 Homebrew
Copyright (c) 2000, 2021, 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.
mysql>
コメント