1. 概要
「Ubuntu 22.04」へ「MySQL」をインストールする機会があったので、記録を残しておきます。
2. インストール
パッケージの更新を行っておきます。
sudo apt update
sudo apt upgrade -y
インストール。
sudo apt install -y mysql-server
インストールが終わった時点で、サービス登録され、起動しているようです。
sudo systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2025-11-18 08:50:43 UTC; 7min ago
Process: 669 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 746 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 4558)
Memory: 426.2M
CPU: 12.456s
CGroup: /system.slice/mysql.service
mq746 /usr/sbin/mysqld
Nov 18 08:50:39 ubuntu2204 systemd[1]: Starting MySQL Community Server...
Nov 18 08:50:43 ubuntu2204 systemd[1]: Started MySQL Community Server.
バージョンは、下記の通りでした。
mysql --version
mysql Ver 8.0.43-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))
3. mysql_secure_installation
これは、「MySQL」をインストールしたことのある人には、おなじみの作業です。
スクリプトを実行して初期化します。
初期化時にパスワード等を設定するのが正式な設定方法です。
sudo mysql_secure_installation
プロンプトに従って入力して進めていきます。
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 Enter で進めます。
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, 1, 2 のいずれかで入力します(レベルは後述)。
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
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) :
「anonymous users」を削除しますかってんで y Enter
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) :
「root」ユーザのリモートログインを禁止しますかってんで y Enter
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 Enter
- 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 Enter
Success.
All done!
これで、基本的な設定は、終わりです。
以前との違いは、わずかなもので、手順的にはほぼ同等だったので、ほっとしました。
いったん、サーバを再起動します。
sudo systemctl restart mysql
4. mysql
「Ubuntu」では、インストール時に、「root」パスワードは設定されていないようです。
「root」ログインするには。
sudo mysql
ここで、早めにパスワードを設定しておきましょう。
5. 設定
コンフィグレーションファイルの構成は、かなり異なります。
下記の構成になっています。
/etc/mysql/
|-- conf.d
| |-- mysql.cnf
| `-- mysqldump.cnf
|-- debian.cnf
|-- debian-start
|-- my.cnf -> /etc/alternatives/my.cnf
|-- my.cnf.fallback
|-- mysql.cnf
`-- mysql.conf.d
|-- mysql.cnf
`-- mysqld.cnf
外部に公開するには。
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
#
# * Basic Settings
#
user = mysql
# pid-file = /var/run/mysqld/mysqld.pid
# socket = /var/run/mysqld/mysqld.sock
# port = 3306
# datadir = /var/lib/mysql
# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
31、32行の「127.0.0.1」を待ち受けするアドレスへ変更します。
複数アドレスを列挙することはできないようで、ループバックアドレスと両方で待つ場合は、「0.0.0.0」を指定するといいようです。
変更したら、再起動します。
sudo systemctl restart mysql
6. 参考サイト
本ページは、「ChatGPT」軍曹を参考にさせていただきました。