CentOs9 Mysqlのインストール手順

CentOs9 Mysqlのインストール手順

CentOs9にMysqlをインストールするまでの手順です。ここではインストールできるバージョン(8.0.30)を確認して「dnf」を使用してインストールしてます。簡単なSQLも記載してます。

環境

  • OS CentOS Stream release 9

Mysqlインストール

まずは「update」を実行しておきます。

$ dnf info mysql

インストールできるバージョンを確認します。

$ dnf info mysql

メタデータの期限切れの最終確認: 0:00:36 時間前の 2022年10月24日 14時20分12秒 に実施しました。
利用可能なパッケージ
名前         : mysql
バージョン   : 8.0.30
リリース     : 3.el9
Arch         : x86_64
サイズ       : 2.8 M
ソース       : mysql-8.0.30-3.el9.src.rpm
リポジトリー : appstream
概要         : MySQL client programs and shared libraries
URL          : http://www.mysql.com
ライセンス   : GPLv2 with exceptions and LGPLv2 and BSD
説明         : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
             : client/server implementation consisting of a server daemon (mysqld)
             : and many different client programs and libraries. The base package
             : contains the standard MySQL client programs and generic MySQL files.

バージョンが「8.0.30」がインストールできるバージョンなので、このままインストールします。

$ sudo dnf install mysql mysql-server

バージョンを確認します

$ mysql --version

mysql  Ver 8.0.30 for Linux on x86_64 (Source distribution)

自動起動の設定と起動します。

$ sudo systemctl enable --now mysqld

設定を別々に行う場合は、以下となります。

## 自動起動
$ sudo systemctl enable mysqld

## 起動
$ sudo systemctl start mysqld

状態は「status」で確認できます。

$ systemctl status mysqld

● mysqld.service - MySQL 8.0 database server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2022-10-24 14:37:16 JST; 3min 52s ago
    Process: 881954 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
    Process: 881976 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
   Main PID: 882059 (mysqld)
     Status: "Server is operational"
      Tasks: 40 (limit: 46459)
     Memory: 455.3M
        CPU: 5.088s
     CGroup: /system.slice/mysqld.service
             └─882059 /usr/libexec/mysqld --basedir=/usr

10月 24 14:37:07 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
10月 24 14:37:07 localhost.localdomain mysql-prepare-db-dir[881976]: Initializing MySQL database
10月 24 14:37:16 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.

再起動は「restart」で可能です。

$ sudo systemctl restart mysqld

初期設定

初期設定を行います。

$ mysql_secure_installation

<出力結果>
## パスワード設定
Press y|Y for Yes, any other key for No: y

## パスワードのポリシー設定 2を選択
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

## rootのパスワードの設定
New password: 
Re-enter new password: 

## 設定したパスワードで問題なければ y
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

## 匿名ユーザーはいらないので y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

## rootでのリモート接続はさせないので y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

## テストDBは不要なので 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!

さきほど作成したパスワードで接続してみます。
※「exit」で抜けることが可能です。

$ mysql -u root -p
Enter password さきほど設定したパスワードを入力

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.30 Source distribution

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.

mysql> exit;
Bye

ログインできれば設定が反映されています。

ちなみに「root」のパスワードは以下のクエリで変更可能です。

mysql>set password for 'root'@'localhost' = '*********';
Query OK, 0 rows affected (0.03 sec)

設定ファイル

mysqlの設定ファイル「my.cnf」は以下の場所にあります。

$ nano /etc/my.cnf

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

簡単なSQL

DBやユーザーの作成は、以下のコマンドで可能です。

# DBの作成

mysql> create database DB名;

# ユーザーの作成

mysql> create user 'ユーザー名'@'localhost' identified with mysql_native_password by 'つけたいパスワード';

# ユーザーに権限を付与

mysql> grant all on `データベース名`.* to 'ユーザー名'@'localhost' with grant option;

# 文字コード確認

mysql> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8mb3                    |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)