suzupy / noresore11

3 stars 0 forks source link

MySQL (MariaDB) 作業一覧 #15

Open suzupy opened 6 years ago

suzupy commented 6 years ago

エキスパートのためのMySQL[運用+管理]トラブルシューティングガイドを読んで重要そうな箇所を抜き出す

suzupy commented 6 years ago

1. mysql_secure_installation

セキュリティに関する基本的な設定を行う

sudo mysql_secure_installation

2. mysqld起動ユーザ変更

root等の広い権限を持つユーザでmysqldを動かしている場合は変更する

# mysqldを動かしているユーザ確認
ps aux | grep mysql | grep -v grep | awk '{print $1}'
id $(ps aux | grep mysql | grep -v grep | awk '{print $1}')

不適切っぽかったら専用ユーザ作成

要reload

sudo mkdir /var/lib/mysql
sudo groupadd mysql
sudo useradd -g mysql -s /bin/false -d /var/lib/mysql mysql
sudo chown mysql:mysql /var/lib/mysql && chmod 700 /var/lib/mysql
sudo passwd mysql
# my.cnfにuser=mysqlを追記
sudo systemctl restart mariadb

3. オプション追加

必要があればmy.cnfに書く

where区の無いDELETEまたはUPDATEを禁止する

MariaDB serverのTCP/IPによる通信を禁止する
WebアプリとDBが同一サーバにあるときに有効にする

MariaDB serverがListenするNICを制限する

4. 不必要なユーザを整理する

どれが不適切かは当日判断

# ユーザ一覧
select Host, User, Password from mysql.user;
# ユーザ権限確認
show grants for USER@HOST;

5. バックアップ

mysqldumpを使う
ダンプ中は全テーブルがRead Onlyになるため, 実行前に確認作業が必要

# dump
sudo mysqldump --all-databases --opt --password=xxx > ./mysql.dump
# restore
mysql -uroot -pxxx < ./mysql.dump
suzupy commented 6 years ago

DB1

MySQL root password, #naresore11MYSQL に変更

suzupy commented 6 years ago

変更前のuser, databases

mysql> select Host, User, Password from mysql.user;
+-----------------------+-------+-------------------------------------------+
| Host                  | User  | Password                                  |
+-----------------------+-------+-------------------------------------------+
| localhost             | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| db1.s00.starbed.local | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| 127.0.0.1             | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| ::1                   | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| localhost             |       |                                           |
| db1.s00.starbed.local |       |                                           |
| 10.%.%.%              | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| 192.168.%.%           | root  | *FB99045441D40F4EC33F53EF87DE2EFA4E2C6549 |
| 127.0.0.1             | mysql | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| 10.%.%.%              | mysql | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| 192.168.%.%           | mysql | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+-----------------------+-------+-------------------------------------------+
11 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| eccube             |
| joomla             |
| mt                 |
| mysql              |
| performance_schema |
| shop               |
| web1               |
| welcart            |
| woo                |
| woocommerce        |
| wordpress          |
| www                |
+--------------------+
13 rows in set (0.01 sec)
suzupy commented 6 years ago
mysql_secure_installation実行 ``` sh [user2@db1 ~]$ sudo mysql_secure_installation [sudo] password for user2: NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, 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 MySQL root user without the proper authorisation. You already have a root password set, so you can safely answer 'n'. Change the root password? [Y/n] n ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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, MySQL 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... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - 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! All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! Cleaning up... [user2@db1 ~]$ ```
suzupy commented 6 years ago

バックアップ

以下のdatabasesのバックアップをとる

mysql> SELECT
    ->     table_schema, sum(data_length) /1024/1024 AS mb
    -> FROM
    ->     information_schema.tables
    -> GROUP BY
    ->     table_schema
    -> ORDER BY
    ->     sum(data_length+index_length) DESC;
+--------------------+--------------+
| table_schema       | mb           |
+--------------------+--------------+
| woocommerce        | 772.78125000 |
| web1               |   6.89451981 |
| mysql              |   0.96127796 |
| information_schema |   0.00000000 |
| performance_schema |   0.00000000 |
+--------------------+--------------+
suzupy commented 6 years ago

バックアップはubuntuの /home/user1/backup/db1 以下に置いた

sudo mysqldump web1 --opt --password=#naresore11 > ./mysql-web1.dump
sudo mysqldump mysql --opt --password=#naresore11 > ./mysql-mysql.dump
sudo mysqldump woocommerce --opt --password=#naresore11 > ./mysql-woocommerce.dump
user1@ubuntu-1:~/backup/db1$ ls -l /home/user1/backup/db1
合計 495416
-rw-rw-r-- 1 user1 user1    739358 11月 23 14:20 mysql-mysql.dump
-rw-rw-r-- 1 user1 user1   8140406 11月 23 14:21 mysql-web1.dump
-rw-rw-r-- 1 user1 user1 498417515 11月 23 14:21 mysql-woocommerce.dump
suzupy commented 6 years ago

DB2

バックアップ

mysql> SELECT
    ->     table_schema, sum(data_length) /1024/1024 AS mb
    -> FROM
    ->     information_schema.tables
    -> GROUP BY
    ->     table_schema
    -> ORDER BY
    ->     sum(data_length+index_length) DESC;
+--------------------+------------+
| table_schema       | mb         |
+--------------------+------------+
| web2               | 1.76562500 |
| entry              | 2.18924332 |
| inquiry            | 1.01232147 |
| mysql              | 0.78885746 |
| information_schema | 0.00000000 |
| performance_schema | 0.00000000 |
+--------------------+------------+
6 rows in set (0.04 sec)

DB1と同じくubuntuに置いた

suzupy commented 6 years ago

Web4のMySQL

バックアップ

mysql> SELECT
    ->     table_schema, sum(data_length) /1024/1024 AS mb
    -> FROM
    ->     information_schema.tables
    -> GROUP BY
    ->     table_schema
    -> ORDER BY
    ->     sum(data_length+index_length) DESC;
+--------------------+------------+
| table_schema       | mb         |
+--------------------+------------+
| wordpress          | 1.90625000 |
| mysql              | 0.78839970 |
| information_schema | 0.00000000 |
| performance_schema | 0.00000000 |
+--------------------+------------+
4 rows in set (0.05 sec)

Ubuntuに

suzupy commented 6 years ago

DB2, 不審なユーザ 'root'@'10.0.0.0/8', 'root'@'192.168.0.0/16' の全権限を剥奪