はじめに
こんにちは、技術部の内藤です。
OCIでMySQLを使う際は、マネージドのHeatWave MySQL Database Service(以下、MDS)を利用することが多いと思いますが、社内サービスなどで重要度が低いかつコストを抑えたいときは、インスタンスに直接MySQLをインストールすることも大いにあると思います。
そこで、今回はOracle Linux9にMySQLをインストールする方法を紹介します。おそらく、Oracle Linux8でも同様の手順だと予想します。
参考情報
以下のMySQL公式ドキュメントを参考にしました。
MySQL :: MySQL 8.0 リファレンスマニュアル :: 2.5.1 MySQL Yum リポジトリを使用して MySQL を Linux にインストールする
インストール手順
sudo権限があるユーザーでログインして実行ください。
# 最新のURL は https://dev.mysql.com/downloads/repo/yum/ でご確認ください
sudo rpm -Uvh http://dev.mysql.com/get/mysql84-community-release-el9-2.noarch.rpm
# デフォルトのモジュールは無効化しておく
sudo dnf module disable mysql
# インストール
sudo dnf install mysql-community-server
# 自動起動を有効化、かつ起動する
sudo systemctl enable --now mysqld
sudo grep password /var/log/mysqld.log
# パスワードを控えておく
# インストール
mysql_secure_installation
# 質問の回答はすべて y を推奨
Securing the MySQL server deployment.
# 控えたパスワードを有効化する
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
# 新しいパスワードを入力する
New password:
# もう一度入力する
Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
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
... 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.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
# localhost以外からのrootログインを禁止する
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
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.
# testというテスト用のデータベースを削除する
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.
# privilege tablesを再読み込みして、ここまでの変更を反映する
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
接続する
ここまで無事に終わったら、以下のコマンドで接続してみましょう。
mysql -u root -p
# パスワードを入力する
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# 無事接続して上記プロンプトが表示されたOK
# データベースやユーザーを作成する場合は以下のようなコマンドで
# データベース作成
CREATE DATABASE IF NOT EXISTS `database_name`;
# ユーザー作成
CREATE USER 'new_user_name'@'localhost' IDENTIFIED BY 'password';
# ユーザーに作成したデータベースの全権限を付与
GRANT ALL PRIVILEGES ON database_name.* TO 'new_user_name'@'localhost';
おわりに
おわりです。