rokkonet

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

ubuntuにmariadbをインストール

2021 Apr. 18.
2021 Mar. 28.
2021 Feb. 02.
2021 Jan. 10.

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

# apt update && apt install  mariadb-server mariadb-client

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

パッケージインストール直後の設定

$ su -
# mysql_secure_installation
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
you haven't set the root password yet, the password will be blank,
so 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 ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] n    ## ubuntuではパスワード認証ではなくunix socket認証になっているので、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] 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? [Y/n] ENTER
 ... 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] 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? [Y/n] ENTER
 ... 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!

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

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

Server version		10.3.25-MariaDB-0ubuntu0.20.04.1
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/run/mysqld/mysqld.sock
Uptime:			25 min 6 sec

Threads: 7  Questions: 473  Slow queries: 0  Opens: 177  Flush tables: 1  Open tables: 31  Queries per second avg: 0.314

UNIX socket接続でのrootユーザーログイン

## linuxシステムのrootユーザーがmariadbのrootユーザー
## ログインパスワード入力不要
# mysql
MariaDB [(none)]> status
--------------
mysql  Ver 15.1 Distrib 10.3.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Connection id:		58
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.3.25-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/var/run/mysqld/mysqld.sock
Uptime:			28 min 52 sec

Threads: 7  Questions: 477  Slow queries: 0  Opens: 177  Flush tables: 1  Open tables: 31  Queries per second avg: 0.275
--------------

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

> select user,host,password from mysql.user;
+------+-----------+-------------+
| user | host      | password |
+------+-----------+-------------+
| root | localhost | *xxxxxxx |
+------+-----------+-------------+

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

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

MariaDB [(none)]> show variables like "colla%";
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+


MariaDB [(none)]> exit;

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

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

DBユーザー作成

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

linux/unixシステムに登録された一般ユーザー名HOGEでのmariaDBユーザーの作成

$ sudo mysql

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


一般ユーザーでのログイン

当該ユーザーでシステムにログインした状態mysqlコマンドを実行する。
DBユーザとlinuxユーザが同名ならばパスワードは不要。

$ mysql