Rubyと筋肉とギターとわたし

筋トレが仕事です

MySQLでユーザーを作成するとAccess Deniedで弾かれる

f:id:rdwbocungelt5:20180718152643p:plain

はい、出ました。

またMySQLのエラー。

zabbixでDB周りの監視をやるための設定をしている時に詰まりました。

ハマった経緯

blog.apar.jp

いつもこちらのサイトを参考にさせてもらってるんですが、 MySQL監視用ユーザー作成の部分で詰まりました。

$ mysql -u root -p
password:

MariaDB [(none)]> grant process on *.* to zabbixagent@localhost identified by '<password>';
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

はい、エラーです。

対処法

まずはmysqlを止める。

$ service mysqld stop

既にzabbixで監視対象にしているので、調査しているときに障害通知メールが来ました。

zabbix優秀ですね。

次に、 --skip-grant-tablesで立ち上げ直す。

$ mysqld_safe --skip-grant-tables

そして、/etc/my.cnf.d/server.cnfにskip-grant-tablesを追加する。

$ vim /etc/my.cnf.d/server.cnf

・
・
・
[mysqld]
・
・
・
skip-grant-tables #=> 僕は最後の行に入れました

[galera]
・
・
・

ここまで終わったら、mysqlを立ち上げ直す。

$ service mysqld start

ここで、mysqlにログインしてみます。--skip-grant-tablesをやっているのでパスワード無しでいけます。

$ mysql -u root

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.1.34-MariaDB MariaDB Server

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)]>

念願のcreate userをやってみると・・・

MariaDB [(none)]> create user zabbixagent identified by 'password';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement

ん〜、だめっぽい。

データベースを見てみると、mysqlってのができているのでみてみると・・・

MariaDB [(none)]> use mysql;

MariaDB[(mysql)]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+

userってとこにありそう。

いろいろ探して、ここを参考に直接userをINSERTしてみました。

ここ↓を参考

stackoverflow.com

MariaDB [mysql]> INSERT INTO mysql.user SET user = 'zabbixagent', host = 'localhost', password = Password('password'),
    -> Select_priv = 'y',
    ->     Insert_priv = 'y',
    ->     Update_priv = 'y',
    ->     Delete_priv = 'y',
    ->     Create_priv = 'y',
    ->     Drop_priv = 'y',
    ->     Reload_priv = 'y',
    ->     Shutdown_priv = 'y',
    ->     Process_priv = 'y',
    ->     File_priv = 'y',
    ->     Grant_priv = 'y',
    ->     References_priv = 'y',
    ->     Index_priv = 'y',
    ->     Alter_priv = 'y',
    ->     Show_db_priv = 'y',
    ->     Super_priv = 'y',
    ->     Create_tmp_table_priv = 'y',
    ->     Lock_tables_priv = 'y',
    ->     Execute_priv = 'y',
    ->     Repl_slave_priv = 'y',
    ->     Repl_client_priv = 'y',
    ->     Create_view_priv = 'y',
  2 # These groups are read by MariaDB server.
    ->     Show_view_priv = 'y',
    ->     Create_routine_priv = 'y',
    ->     Alter_routine_priv = 'y',
    ->     Create_user_priv = 'y',
    ->     Event_priv = 'y',
    ->     Trigger_priv = 'y',
    ->     Create_tablespace_priv = 'y';
Query OK, 1 row affected, 4 warnings (0.00 sec)

おっ、いけた。

userテーブルを見てみると先ほど入れたユーザーが入ってました。

いとをかし。

あと始末

先ほど追加した/etc/my.cnf.d/server.cnfskip-grant-tablesを削除し、mysqlを起動し直します。

これでおkです。