0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ubuntu 22.04 に MySQL 8.0 をインストール

Posted at

自分用メモ。

構築環境

以下の環境にHyper-V上の仮想マシンに構築します。

  • Hyper-V VM
  • OS : Ubuntu 22.04 LTS
  • IPアドレス : 192.168.1.201

MySQL最新版インストール

まずは何はともあれMySQLのインストール。

dbuser@dbserver:~$ sudo apt update
dbuser@dbserver:~$ sudo apt install mysql-server
dbuser@dbserver:~$ mysql --version
mysql  Ver 8.0.35-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

記事作成時のMySQLの最新は8.0.35。なので以下はそれに伴う記載をします。

MySQLにユーザ作成

MySQLにrootユーザでログインします。

dbuser@dbserver:~$ sudo mysql -u root

ユーザを作成します。
今回はtestuserを作成。
testuserpassword%は環境により変更します。

mysql> CREATE USER 'testuser'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.02 sec)

さて、これでユーザは作成できました。
ただこのtestuserでは、外部から接続することもできません。
例えば同じLAN上のPCから以下のようにしても接続できません。

$ mysql -u testuser -p -h 192.168.1.201
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.201:3306' (111)

これは初期設定では、mysqld.cnfでDBがlocalhostのみにバインドされているため。
自機からのみ接続されるならこれでも良いのでしょうが、私は外部から接続したい。
そのため以下のmysqld.cnfの編集に続きます。

mysqld.cnf の編集

mysqld.cnfにあるbind-addressをコメントアウトし、どのIPアドレスからもアクセスできるようにします。
その後、MySQLを再起動します。

dbuser@dbserver:~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
dbuser@dbserver:~$ sudo service mysql restart

これで設定が反映されたはずです。
手元のPCに戻って再試行します。

$ mysql -u testuser -p -h 192.168.1.201
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)

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>

無事接続できました。

ufw設定

ufwで使用するポートを開放・設定していきます。
環境によって開くポートは各々違いますが、ここではSSHの22番およびMySQLの3306番のみ、192.168.1.0/24からの接続を許可していきます。

$ sudo ufw status
Status: inactive
$ sudo ufw allow from 192.168.1.0/24 to any port 22
Rules updated
$ sudo ufw allow from 192.168.1.0/24 to any port 3306
Rules updated
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
$ sudo ufw status
Status: active
To                         Action      From
--                         ------      ----
22                         ALLOW       192.168.1.0/24
3306                       ALLOW       192.168.1.0/24

これについてもより詳しい設定は色々あると思いますが、ここではMySQL(とssh)で接続する最低限の設定としています。

参考

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?