CentOS7でmariadbをインストール

PostgreSQL使いだったのですが、異動した先ではmariadbを使用していた(といっても、ZabbixのバックエンドDBくらいしかないのですが)ので、mariadbも触っておこうと...

OS: CentOS Linux release 7.3.1611 (Core)
mariadb: 5.5.52

developというユーザーを作って、DB操作を外の端末からできるようにするまでのメモです。

1. yumからパッケージをインストール

# yum install mariadb-server

自分のテスト環境では、依存関係でmariadb、perlなどもインストールされました。


2. サーバーのキャラクタセットをutf-8にする

# vi /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

character-set-server = utf8

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d


3. 自動起動設定とデーモン起動



# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

# systemctl start mariadb
#
# ps -ef

mysql    30495 30325  0 22:58 ?        00:00:02 /usr/libexec/mysqld ...



4. セキュアな設定


# 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
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):
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] Y
New password: パスワード
Re-enter new password:
パスワード 

Password updated successfully!
Reloading privilege tables..
 ... Success!


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!




5. 接続してみる


# mysql -u root -p
Enter password: パスワード
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.52-MariaDB MariaDB Server

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

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

MariaDB [(none)]> select host,user from mysql.user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)


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

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




6. ユーザー、データベースを作成してみる

MariaDB [(none)]> create user 'develop'@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select host,user from mysql.user;
+-----------+---------+
| host      | user    |
+-----------+---------+
| %         | develop |
| 127.0.0.1 | root    |
| ::1       | root    |
| localhost | root    |
+-----------+---------+
4 rows in set (0.01 sec)
MariaDB [(none)]> create database devel_db default character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

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

MariaDB [(none)]> grant all on devel_db.* to 'develop'@'%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

MariaDB [(none)]> \q
Bye

# mysql -u develop -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.52-MariaDB MariaDB Server

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

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

MariaDB [(none)]>
MariaDB [(none)]> use devel_db
Database changed

MariaDB [devel_db]> begin;
Query OK, 0 rows affected (0.00 sec)

MariaDB [devel_db]>
MariaDB [devel_db]> create table test(
    -> test varchar(10)
    -> );

Query OK, 0 rows affected (0.03 sec)

MariaDB [devel_db]> insert into test values('hello!');
Query OK, 1 row affected (0.01 sec)

MariaDB [devel_db]> select * from test;
+--------+
| test   |
+--------+
| hello! |
+--------+
1 row in set (0.00 sec)

MariaDB [devel_db]> commit;
Query OK, 0 rows affected (0.00 sec)

MariaDB [devel_db]> \q
Bye




7. 外部から接続してみる

HeidiSQL(https://www.heidisql.com/)を利用してデータベースにアクセスしました。



コメント