Vagrant+AlmaLinuxで構築した開発環境にMySQL8をインストールする
こんばんわ。
久々の休日出勤(リモート)をこなして精神的な消耗が激しい、どうもみけぽんです。
前回の記事ではAlmaLinux環境にLaravelをインストールしたので、今回はMySQLをインストールしようと思います。
Vagrant+AlmaLinuxで構築した開発環境にMySQL8をインストールする
前回の記事
MySQLのインストール
モジュール、本体のインストール
まずはリリースパッケージをインストールします。
$ dnf localinstall -y https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
そしてMySQLのインストール。
$ dnf install -y @mysql:8.0
インストール後にバージョンを確認します。
$ mysql --version
mysql Ver 8.0.21 for Linux on x86_64 (Source distribution)
その後、MySQLの起動と有効化を行います。
$ systemctl start mysqld
$ systemctl enable mysqld
エラーメッセージが表示されなければOKです。
インストール後の初期設定
パスワードポリシーの変更
デフォルトだとパスワードポリシーが強く、下記のルールが課せられています。
- 8文字以上
- 大文字小文字 1文字以上
- 数字 1文字以上
- 記号 1文字以上
個人的に上記のままだとパスワードポリシーが強すぎるので、設定ファイルにパスワードポリシーを弱める一文を末尾に追加します。
$ vi /etc/my.cnf
validate_password.policy=LOW
その後、MySQLを再起動します。
$ systemctl restart mysqld
rootパスワード設定
下記コマンドを実行すると対話形式でパスワード設定が進められます。
$ mysql_secure_installation
新しいパスワードを確認用も含めて入力し、問題がなければyを入力します。
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 50y
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
anonymous usersは削除します。
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
念のためセキュリティを考慮し、他マシンからのrootログインは不可にします。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
テストデータベースは不要なので削除します。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
設定を反映させたいのでyで実行します。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
All done!が表示されれば完了です。
ログイン後の設定
まずはMySQLにログイン
MySQLにrootユーザーでログインします。
$ mysql -u root -p
Enter password:
Laravel用のデータベース、ユーザーの作成
Laravelでは.envファイルにデータベースの接続情報を記載することになりますが、rootユーザーのパスワードを平文で記載するのはセキュリティ的にアウトです。
ここではLaravel用のデータベースとユーザーを作成するので、.envにはその情報を記載するようにします。
> create database hoge_db;
> create user 'user_name'@'localhost' identified by 'password';
作成したユーザーに、作成したデータベースの権限を付与します。
その後flush privileges
を実行して反映させてください。
> grant all on hoge_db.* to 'user_name'@'localhost' with grant option;
> flush privileges;
まとめ
CentOS8とほぼ変わらないコマンドで、PHP8、Laravel、MySQL8をインストールすることが出来ました。
Ubuntuはやはり好きになれないので、自宅環境は今のところAlmaLinuxで開発を進めていこうと思います。