Ubuntu19.04にMariaDBをインストール

Ubuntu19.04にMariaDBをインストール

Ubuntu19.04にMariaDBをインストールする手順。けっこうハマったので、手順をメモしてます。

Ubuntuバージョン

ubuntu19.04

MariaDBのインストール

## 面倒なのでrootで作業する
sudo su -

## MariaDBのインストール
apt install mariadb-server  mariadb-client

## バージョン確認
mysql -v

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 47
Server version: 10.3.17-MariaDB-0ubuntu0.19.04.1 Ubuntu 19.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others

## 設定ファイル確認
vim /etc/mysql/mariadb.conf.d/50-server.cnf

[mysqld]
utf8mb4なのを確認
character-set-server=utf8mb4

初期設定

mysql_secure_installation 

# 初期はROOTパスワードが無いのでEnter
Enter current password for root (enter for none): # Enterキー押下

# rootパスワードを設定しますか
Set root password? [Y/n] # Enterキー押下

# 新しいrootパスワードを設定
New password: # 新しいパスワードの入力

# 新しいパスワードの確認
Re-enter new password: # 新しいパスワードを再入力

# 匿名ユーザのログインを削除しますか
Remove anonymous users? [Y/n] # Enterキー押下

# rootでのリモートログインを禁止します
Disallow root login remotely? [Y/n] # Enterキー押下

# testデータベースを削除しますか
Remove test database and access to it? [Y/n]  # Enterキー押下

# 権限管理テーブルをリロードしますか
Reload privilege tables now? [Y/n] # Enterキー押下

Rootでログインしてみる

## ログイン
mysql -u root -p

## パスワードが聞かれるので先ほど設定したパスワードを入力
Enter password:

## ログイン成功
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 56
Server version: 10.3.17-MariaDB-0ubuntu0.19.04.1 Ubuntu 19.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

firewallの設定

外部から接続が必要な場合

## 3306ポート追加
ufw allow 3306
 
## 再起動
ufw reload
 
## 確認
ufw status

状態: アクティブ

To                         Action      From
--                         ------      ----
2022/tcp                   ALLOW       Anywhere
80                         ALLOW       Anywhere
3306                       ALLOW       Anywhere

接続テスト

## 接続
mysql -u root -p

## ユーザー作成
MariaDB [(none)]> CREATE USER 'dbtestuser'@'192.168.101.%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.000 sec)

## 権限設定
MariaDB [(none)]> GRANT ALL ON *.* TO 'dbtestuser'@'192.168.101.%';
Query OK, 0 rows affected (0.000 sec)

## 接続テスト
mysql -u dbtestuser -h 192.168.101.XXX -p
Enter password: password

ERROR 2002 (HY000): Can't connect to MySQL server on

繋がらない。。。

原因調査

# FW見直し
ufw allow 3306
すでに存在するルールは追加せずに飛ばします

## 再起動忘れ?
ufw reload

問題なさそう。でもまだ繋がらないので利用ポートが間違えないか確認

## 利用ポートの確認
lsof -i:3306
COMMAND PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  588 mysql   21u  IPv6 173877      0t0  TCP *:mysql (LISTEN)

3306が利用されている。selinuxが入ってる?

## 確認
getenforce

Command 'getenforce' not found, but can be installed with:

apt install selinux-utils

インストールされてない。iptablesも確認

iptables -L

Chain ufw-user-input (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:2022
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     udp  --  anywhere             anywhere             udp dpt:80
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:mysql
ACCEPT     udp  --  anywhere             anywhere             udp dpt:mysql
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:mysql

問題なさそう。何度もインストールした経験ありますが、こんなとこで、ハマったのは初めて。もう一度、設定ファイルを見直す

## 設定ファイル確認
vim /etc/mysql/mariadb.conf.d/50-server.cnf

# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
↓
# bind-address            = 127.0.0.1

bind-address がデフォルトでコメントアウトされてない。。。これが原因っぽいのでコメントアウト

bind-addressは接続したいマシンのIPを記載すれば接続でき、どのマシンからも接続するためにはbind-addressをコメントアウトする必要がある
【記載例】
bind-address = 接続したいマシンのIPアドレス
## 接続
mysql -u dbtestuser -h サーバー側IP -p

けっこう手こずったが、無事接続