LoginSignup
2
6

More than 5 years have passed since last update.

MariaDB で ERROR 2002 (HY000): Can't connect to local MySQL server が発生する際の対処法

Last updated at Posted at 2018-05-20

以下のような ERROR 2002 で MariaDB に接続できないエラーに遭遇した場合の対処法を以下にまとめてみました。

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

OSはCentOS 7、MariaDBのバージョンはOSに標準でバンドルされている 5.5.x という前提ですすめます。

そもそも mysqld が起動していない

ps -ef | grep -i mysqld

で mysqld が稼働しているか確認してください。起動していない場合、

systemctl enable mariadb
systemctl start mariadb

を実行してみてください。正常に起動している場合の ps -ef | grep mysqldの出力は以下のようになります。

# ps -ef|grep mysqld
mysql     2941     1  0 12:09 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     3103  2941  0 12:09 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      3411  2449  0 12:27 pts/1    00:00:00 grep --color=auto mysqld

/var/lib/mysql 配下のファイルの owner が mysqld 実行ユーザ(mysql)になっていない

drwx------.  2 root  root         6 May 20 12:07 test
drwx------.  2 root  root      4096 May 20 12:07 mysql
drwx------.  2 root  root      4096 May 20 12:07 performance_schema
-rw-rw----.  1 root  root        52 May 20 12:07 aria_log_control
-rw-rw----.  1 root  root     16384 May 20 12:07 aria_log.00000001
drwxr-xr-x.  5 mysql mysql      159 May 20 12:07 .
-rw-rw----.  1 mysql mysql  5242880 May 20 12:07 ib_logfile1
-rw-rw----.  1 mysql mysql  5242880 May 20 12:07 ib_logfile0
-rw-rw----.  1 mysql mysql 18874368 May 20 12:07 ibdata1

以下のコマンドを実行してみてください。

chown -R mysql:mysql /var/lib/mysql
systemctl start mariadb

これで mysql コマンドで接続できるようになると思います。

# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> 

firewall 設定で port 3306 が開放されていない

ScreenShot_20180520212352.png

AWS EC2 では security group, GCE では firewall rule などで、tcp port 3306 が開放されていることを確認してください。

(おまけ)MariaDB 10.2 に更新

現時点での最新版 10.2 に更新してみます。

yum -y remove "mariadb*"
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
yum -y install MariaDB-server

エラーログが出力されるように mysqld セクションに log_error を設定します。

/etc/my.cnf.d/server.cnf
[mysqld]
log_error

mariadb サービスを起動しますと、

systemctl start mariadb

/var/lib/mysql/hostname.err にエラーログが出力されます。

2018-05-20 12:37:04 139889543854272 [ERROR] Missing system table mysql.roles_mapping; please run mysql_upgrade to create it

mysql_upgrade を実行するように促されていますので、mysql_upgradeを実行します。

# mysql_upgrade
Phase 1/7: Checking and upgrading mysql database
Processing databases
mysql
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.event                                        OK
mysql.func                                         OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.servers                                      OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
Upgrading from a version before MariaDB-10.1
Phase 2/7: Installing used storage engines
Checking for tables with unknown storage engine
Phase 3/7: Fixing views
Phase 4/7: Running 'mysql_fix_privilege_tables'
Phase 5/7: Fixing table and database names
Phase 6/7: Checking and upgrading tables
Processing databases
information_schema
performance_schema
test
test.t1                                            OK
Phase 7/7: Running 'FLUSH PRIVILEGES'
OK

mariadb サービスを再起動します。

systemctl restart mariadb
/var/lib/mysql/server.error
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Uses event mutexes
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Compressed tables use zlib 1.2.7
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Using Linux native AIO
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Number of pools: 1
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Using SSE2 crc32 instructions
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Completed initialization of buffer pool
2018-05-20 12:47:05 140364988831488 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Highest supported file format is Barracuda.
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: 128 out of 128 rollback segments are active.
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Creating shared tablespace for temporary tables
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: Waiting for purge to start
2018-05-20 12:47:05 140365761669312 [Note] InnoDB: 5.7.22 started; log sequence number 1624410
2018-05-20 12:47:05 140364748936960 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2018-05-20 12:47:05 140364748936960 [Note] InnoDB: Buffer pool(s) load completed at 180520 12:47:05
2018-05-20 12:47:05 140365761669312 [Note] Plugin 'FEEDBACK' is disabled.
2018-05-20 12:47:05 140365761669312 [Note] Server socket created on IP: '::'.
2018-05-20 12:47:05 140365761669312 [Note] Reading of all Master_info entries succeded
2018-05-20 12:47:05 140365761669312 [Note] Added new Master_info '' to hash table
2018-05-20 12:47:05 140365761669312 [Note] /usr/sbin/mysqld: ready for connections.
Version: '10.2.15-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server

mysql_upgrade で各テーブルを更新したことにより、エラーが発生しなくなりました。

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6