rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

MariaDBをubuntuにインストール

2024 Jan. 04.
2023 Dec. 31.
2021 Apr. 18.
2021 Mar. 28.
2021 Feb. 02.
2021 Jan. 10.

パッケージをインストール

$ sudo apt update && sudo apt install mariadb-server mariadb-client

mariadbの稼働確認
$ systemctl status mariadb.service

動いていなければ
$ sudo systemctl enable mariadb.service
$ systemctl restart mariadb.service

パッケージインストール直後の作業

$ su -
# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): Enterキーのみを押す
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!

By default, MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
# exit

バージョン確認、接続確認

$ sudo mysqladmin version
mysqladmin  Ver 9.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.6.12-MariaDB-0ubuntu0.22.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /run/mysqld/mysqld.sock
Uptime:                 15 min 52 sec

Threads: 1  Questions: 70  Slow queries: 0  Opens: 33  Open tables: 26  Queries per second avg: 0.073

rootユーザーのUNIX socket接続での利用

linuxシステムのrootユーザーがmariadbのrootユーザー。
ログインパスワード入力不要。

$ sudo mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 39
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.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)]> status
--------------
mysql  Ver 15.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

Connection id:          39
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb3
Conn.  characterset:    utf8mb3
UNIX socket:            /run/mysqld/mysqld.sock
Uptime:                 18 min 58 sec

Threads: 1  Questions: 74  Slow queries: 0  Opens: 33  Open tables: 26  Queries per second avg: 0.065
--------------

MariaDB [(none)]> show grants for root@localhost;
+-----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost
                  |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION
                  |
+-----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.001 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+-------------+-----------+----------+
| User        | Host      | Password |
+-------------+-----------+----------+
| mariadb.sys | localhost |          |
| root        | localhost |          |
| mysql       | localhost |          |
+-------------+-----------+----------+
3 rows in set (0.004 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.001 sec)

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

MariaDB [(none)]> show variables like "colla%";
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb3_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> exit


一般ユーザーのUNIX socket接続での利用

参考 Ubuntu 18.04 + MariaDB | 技術メモの壁

root権限で一般ユーザー名のDBユーザーを作成した上で、一般ユーザー権限でmysqlコマンドを入力する。

(1) DBユーザー作成

DBユーザをUNIXユーザと同名で作成すれば、ローカルの UNIX ドメインソケット接続される。
この接続方式では外部ホストからTCP/3306経由でのログインはできない。(リモートからはssh接続すればよい)

linuxシステムに登録された一般ユーザーHOGEで、mariaDBユーザーHOGEunix socketで作成する

$ sudo mysql

MariaDB [(none)]> CREATE USER HOGE@localhost IDENTIFIED VIA unix_socket ;
MariaDB [(none)]>  flush privileges;
MariaDB [(none)]>  exit


(2) 一般ユーザーでログイン

引数なしでmysqlコマンドを実行する。
DBユーザとlinuxユーザが同名ならばパスワードは不要。

$ mysql