LoginSignup
1
0

はじめてのWebサーバでデータベースを利用①(さくらのVPS, CentOS Stream 9, MySQL, Java 11, Tomcat 9, サーブレット&JSP)

Posted at

はじめてのWebサーバでデータベースを利用①

1. 前提条件

既に導入が済んでいるもの

  • さくらのVPS[サーバ申込]
  • CentOS Stream 9[さくらのVPS上にインストール済]
  • Tera Term 5 [インストール済]
  • ドメイン導入[お名前.com利用しました]
  • JavaEE 11
  • tomcat 9.0.88

これらの設定については、はじめてのWebサーバ構築(centOS Stream 9, Java 11, tomcat 9, サーブレット)で記事にしましたので、良かったら参考にしてください。

このページで設定するもの

  • MySQL

2. MySQLのインストール

まず、今回参考にさせてもらったページです。

CentOS Stream 9 にDNF で MySQL を公式からインストール

MySQL公式リポジトリのインストール

まずは、root権限に切り替えます。

sudo -s

MariaDBがインストールされていないことを確認し、
dnf のmysqlのバージョンを確認します。
ちなみに私は 8.0.36 でした。

dnf list installed | grep mariadb
dnf info mysql

「公式リポジトリ」をインストールします。

dnf localinstall https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm

この中にある84と、9-1の部分は、公式サイトから確認し、適宜変更してください。

MySQLの「MySQL Community Server」をインストール

dnf install mysql-community-server

3. MySQLの起動と自動起動設定

次に自動起動設定を行い、起動します。

# 自動起動設定
systemctl enable mysqld

# 起動
systemctl start mysqld

4. パスワードの確認

head -n 5 /var/log/mysqld.log

自分は5行目に表示されました。

A temporary password is generated for root@localhost: XXXXXXXX

XXXXXXXX に書かれているのが root の初期パスワードです。

5. 初期設定

mysql_secure_installation

以下は参考にさせていただいたページそのままの表記です。これ以上なく分かりやすい表記でしたので、感謝のうえそのまま記載させていただきます。

rootパスワードを入力 ※4.で確認したもの

Enter password for user root:

rootの新しいパスワード

※英(大小)数記号が混ざってないとダメ
The existing password for the user account root has expired. Please set a new password.

パスワードの強度 100

Estimated strength of the password: 100

root のパスワードを変更するか

※はいなら y いいえなら他のキー
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

入力したパスワードでOKか?

※はいなら y いいえなら他のキー
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :

匿名ユーザーを削除するか?

※はい(削除する)なら y いいえなら他のキー
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

リモートからの root へのログインを禁止するか

※はい(禁止する)なら y いいえなら他のキー
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 いいえなら他のキー
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

6. 実際に接続してみる。

mysql -u root -p

rootで接続、先ほど設定したパスワードを入力してください。

以下のような表示が出てくれば成功です。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.4.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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」になっているはずです。

7. おわりに

今回はここまでとします。
実際にテーブルを作成、レコードを入力しサーブレットで利用するのを次回、実行したいと思います。

誰かのお役に立ちますように。

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