【簡単】LinuxにMySQLインストール〜接続方法

IT技術

概要

Linuxにオープンソースのデータベースソフトウェア「MySQL」をインストールする方法と、その後のサービス起動/停止、接続方法を記載する。

システム環境

  • Oracle Linux Server 8.5
  • MySQL Server 8.0.32

インストール〜初期設定

インストール

「yum install mysql-server.x86_64」コマンドを使用してインストールする。

[root@testvm999 ~]# yum install mysql-server.x86_64
Last metadata expiration check: 3:17:26 ago on Sat 10 Jun 2023 02:07:51 AM GMT.
Dependencies resolved.
================================================================================================================================
 Package                  Architecture      Version                                              Repository                Size
================================================================================================================================
Installing:
 mysql-server             x86_64            8.0.32-1.module+el8.8.0+21055+76bd398b               ol8_appstream             32 M
Installing dependencies:
 mecab                    x86_64            0.996-2.module+el8.8.0+21055+76bd398b                ol8_appstream            394 k
 mysql                    x86_64            8.0.32-1.module+el8.8.0+21055+76bd398b               ol8_appstream             15 M
 mysql-common             x86_64            8.0.32-1.module+el8.8.0+21055+76bd398b               ol8_appstream            137 k
 mysql-errmsg             x86_64            8.0.32-1.module+el8.8.0+21055+76bd398b               ol8_appstream            629 k
 protobuf-lite            x86_64            3.5.0-15.el8                                         ol8_appstream            149 k
Enabling module streams:
 mysql                                      8.0                                                                                
Transaction Summary
================================================================================================================================
Install  6 Packages
Total download size: 48 M
Installed size: 245 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): mysql-common-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64.rpm                           1.1 MB/s | 137 kB     00:00    
(2/6): mecab-0.996-2.module+el8.8.0+21055+76bd398b.x86_64.rpm                                   1.5 MB/s | 394 kB     00:00    
(3/6): mysql-errmsg-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64.rpm                           1.8 MB/s | 629 kB     00:00    
(4/6): mysql-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64.rpm                                   16 MB/s |  15 MB     00:00    
(5/6): protobuf-lite-3.5.0-15.el8.x86_64.rpm                                                    337 kB/s | 149 kB     00:00    
(6/6): mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64.rpm                            21 MB/s |  32 MB     00:01    
--------------------------------------------------------------------------------------------------------------------------------
Total                                                                                            27 MB/s |  48 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                        1/1 
  Installing       : mysql-common-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             1/6 
  Installing       : mysql-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                                    2/6 
  Installing       : mysql-errmsg-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             3/6 
  Installing       : protobuf-lite-3.5.0-15.el8.x86_64                                                                      4/6 
  Installing       : mecab-0.996-2.module+el8.8.0+21055+76bd398b.x86_64                                                     5/6 
  Running scriptlet: mecab-0.996-2.module+el8.8.0+21055+76bd398b.x86_64                                                     5/6 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             6/6 
  Installing       : mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             6/6 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             6/6 
ValueError: File context for /var/log/mysql(/.*)? already defined
  Verifying        : mecab-0.996-2.module+el8.8.0+21055+76bd398b.x86_64                                                     1/6 
  Verifying        : mysql-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                                    2/6 
  Verifying        : mysql-common-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             3/6 
  Verifying        : mysql-errmsg-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             4/6 
  Verifying        : mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64                                             5/6 
  Verifying        : protobuf-lite-3.5.0-15.el8.x86_64                                                                      6/6 
Installed:
  mecab-0.996-2.module+el8.8.0+21055+76bd398b.x86_64             mysql-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64           
  mysql-common-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64     mysql-errmsg-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64    
  mysql-server-8.0.32-1.module+el8.8.0+21055+76bd398b.x86_64     protobuf-lite-3.5.0-15.el8.x86_64                             
Complete!
[root@testvm999 ~]# 

初期設定

MySQLの設定ファイルを変更し、初期設定を行う。

MySQL設定ファイル本体は「/etc/my.cnf」である。

[root@testvm999 ~]# cat /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
[root@testvm999 ~]# 

「/etc/my.cnf」の中にデフォルトで「!includedir /etc/my.cnf.d」という設定が入っており、「/etc/my.cnf.d」ディレクトリ配下に存在する設定ファイルを読み込む形になっている。

「/etc/my.cnf.d」ディレクトリ配下にはデフォルトで以下の設定ファイルが存在する。

[root@testvm999 ~]# ls -l /etc/my.cnf.d
total 12
-rw-r--r--. 1 root root 295 Dec 15  2020 client.cnf
-rw-r--r--  1 root root 565 May 19 12:52 mysql-default-authentication-plugin.cnf
-rw-r--r--  1 root root 612 May 19 12:57 mysql-server.cnf
[root@testvm999 ~]# 

この中でサーバ用の設定ファイル「mysql-server.cnf」とクライアント用の設定ファイル「client.cnf」を変更する。

サーバ用の設定ファイル「mysql-server.cnf」

今回は★部パラメータを追加した。

[root@testvm999 ~]# vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
character-set-server = utf8 ★追加
skip-character-set-client-handshake ★追加
default-storage-engine=INNODB ★追加
innodb_file_per_table ★追加
[root@testvm999 ~]# 

各パラメータの意味は次の通りである。

  • character-set-server = utf8
    文字コードをUTF-8に設定する。
  • skip-character-set-client-handshake
    MySQL内部で文字コードを自動変換する機能をOFFにする。(不測な文字化け対策)
  • default-storage-engine=INNODB
    デフォルトのストレージエンジンをINNODBに設定する。
  • innodb_file_per_table
    INNODBの各テーブルのデータとインデックスを個別のファイル管理にする。
    もしこのオプションが無効化されていると、ibdata1という単一ファイルに全てのデータが格納され、データ肥大化時の縮小対応は難しくなる。

クライアント用の設定ファイル「client.cnf」

今回は★部パラメータを追加した。

[root@testvm999 ~]# vi /etc/my.cnf.d/client.cnf 
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
default-character-set = utf8 ★追加
# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
[root@testvm999 ~]# 

各パラメータの意味は次の通りである。

  • default-character-set = utf8
    文字コードをUTF-8に設定する。

サービス起動/停止

systemctlコマンドを使用してサービスの起動/停止を行う。

サービス起動

[root@testvm999 ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@testvm999 ~]# systemctl start mysqld
[root@testvm999 ~]# 
[root@testvm999 ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2023-06-10 05:57:45 GMT; 10s ago
  Process: 2401721 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 2401493 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 2401468 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2401675 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 3927)
   Memory: 406.2M
   CGroup: /system.slice/mysqld.service
           └─2401675 /usr/libexec/mysqld --basedir=/usr
Jun 10 05:57:24 testvm999 systemd[1]: Starting MySQL 8.0 database server...
Jun 10 05:57:24 testvm999 mysql-prepare-db-dir[2401493]: Initializing MySQL database
Jun 10 05:57:45 testvm999 systemd[1]: Started MySQL 8.0 database server.
[root@testvm999 ~]# 

サービス停止

[root@testvm999 ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2023-06-10 05:57:45 GMT; 1h 9min ago
  Process: 2401721 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 2401493 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 2401468 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2401675 (mysqld)
   Status: "Server is operational"
    Tasks: 40 (limit: 3927)
   Memory: 250.4M
   CGroup: /system.slice/mysqld.service
           └─2401675 /usr/libexec/mysqld --basedir=/usr
Jun 10 05:57:24 testvm999 systemd[1]: Starting MySQL 8.0 database server...
Jun 10 05:57:24 testvm999 mysql-prepare-db-dir[2401493]: Initializing MySQL database
Jun 10 05:57:45 testvm999 systemd[1]: Started MySQL 8.0 database server.
[root@testvm999 ~]# 
[root@testvm999 ~]# systemctl stop mysqld
[root@testvm999 ~]# 
[root@testvm999 ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
Jun 10 05:57:24 testvm999 systemd[1]: Starting MySQL 8.0 database server...
Jun 10 05:57:24 testvm999 mysql-prepare-db-dir[2401493]: Initializing MySQL database
Jun 10 05:57:45 testvm999 systemd[1]: Started MySQL 8.0 database server.
Jun 10 07:07:42 testvm999 systemd[1]: Stopping MySQL 8.0 database server...
Jun 10 07:07:44 testvm999 systemd[1]: mysqld.service: Succeeded.
Jun 10 07:07:44 testvm999 systemd[1]: Stopped MySQL 8.0 database server.
[root@testvm999 ~]# 

最低限のセキュリティ設定

mysql_secure_installationコマンドを使用して、最低限のセキュリティ設定を行う。
対話形式で管理者ユーザ(root)のパスワード設定や、初期作成されるtestデータベースの削除などを行う。

[root@testvm999 ~]# mysql_secure_installation
mysql_secure_installation: [ERROR] unknown variable 'default-character-set=utf8'.
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
 ★パスワードの文字列制限を設定するか聞かれる。設定するため"Y"を入力。
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: 0
 ★文字数制限のレベルをLOW〜STRONGのどれにするか聞かれる。今回はLOWにするため"0"を入力。
Please set the password for root here.
New password: 
Re-enter new password: 
 ★管理者ユーザのパスワードを入力。
Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
 ★確認メッセージ。"Y"を入力。
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
 ★初期作成された匿名ユーザを削除するか聞かれる。
  セキュリティ上、削除したほうが良いため"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.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
 ★ネットワーク越しにリモートから管理者ユーザのログインを拒否するか聞かれる。
  セキュリティ上、管理者ユーザはローカル接続のみに限定するため"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.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 ★初期作成されたtestデータベースを削除するか聞かれる。不要なため"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
 ★ここまで設定した権限情報を再読み込みするか聞かれる。"Y"を入力。
Success.
All done! 
[root@testvm999 ~]# 

MySQLに接続

mysqlコマンドを使用してMySQLに接続する。
管理者ユーザ(root)を指定してログインする。パスワードは上述のmysql_secure_installationコマンドを使用して設定したパスワードを使用する。

[root@testvm999 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.32 Source distribution
Copyright (c) 2000, 2023, 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@testvm999 ~]# 
タイトルとURLをコピーしました