MariaDB : ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111 “Connection refused”)
注意:如果你是因为没有设置允许远程访问,请参考这篇文章:debian,ubuntu下安装MariaDB,并设置密码,修改端口,允许外网访问
早上想连接到数据库时报错如下(我把数据库的端口改成了51009,防止被人发现):
-> # mysql -h 127.0.0.1 -P 51009 -u root -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
我用的是MariaDB,我的问题跟其他人的不同,我是前一天还是可以连接的,然后今天就连接不上了。
然后查看端口情况:
-> # nmap -p 51009 127.0.0.1
Starting Nmap 7.40 ( https://nmap.org ) at 2019-04-04 09:15 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000062s latency).
PORT STATE SERVICE
51009/tcp closed unknown
发现端口关闭了,我觉得事情不对,于是查看MariaDB 的状态:
-> # service mysql status
● mariadb.service - MariaDB 10.1.37 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2019-04-04 07:11:25 CST; 2h 15min ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 20933 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 20931 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
Process: 6808 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=1/FAILURE)
Process: 6731 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSRE
Process: 6725 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 6724 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
Main PID: 6808 (code=exited, status=1/FAILURE)
Status: "MariaDB server is down"
CPU: 312ms
Apr 04 07:11:24 VM-73-203-debian systemd[1]: mariadb.service: Service hold-off time over, scheduling restart.
Apr 04 07:11:24 VM-73-203-debian systemd[1]: Stopped MariaDB 10.1.37 database server.
Apr 04 07:11:24 VM-73-203-debian systemd[1]: Starting MariaDB 10.1.37 database server...
Apr 04 07:11:24 VM-73-203-debian mysqld[6808]: 2019-04-04 7:11:24 139734049481152 [Note] /usr/sbin/mysqld (mysqld 10.1.37-MariaDB-0+deb9u1) starting as process 6808 ..
Apr 04 07:11:25 VM-73-203-debian systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE
Apr 04 07:11:25 VM-73-203-debian systemd[1]: Failed to start MariaDB 10.1.37 database server.
Apr 04 07:11:25 VM-73-203-debian systemd[1]: mariadb.service: Unit entered failed state.
Apr 04 07:11:25 VM-73-203-debian systemd[1]: mariadb.service: Failed with result 'exit-code'.
发现MariaDB 发生异常,直接退出了。
查看更详细的报错信息:
-> # cat /var/log/mysql/error.log
2019-04-04 7:11:24 139734049481152 [ERROR] mysqld: Out of memory (Needed 128663552 bytes)
2019-04-04 7:11:24 139734049481152 [Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffer pool size. In order to use backoff, increase buffer pool at least up to 20MB.
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: The InnoDB memory heap is disabled
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Compressed tables use zlib 1.2.8
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Using Linux native AIO
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Using SSE crc32 instructions
2019-04-04 7:11:25 139734049481152 [Note] InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(140574720 bytes) failed; errno 12
2019-04-04 7:11:25 139734049481152 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2019-04-04 7:11:25 139734049481152 [ERROR] Plugin 'InnoDB' init function returned error.
2019-04-04 7:11:25 139734049481152 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2019-04-04 7:11:25 139734049481152 [Note] Plugin 'FEEDBACK' is disabled.
2019-04-04 7:11:25 139734049481152 [ERROR] Unknown/unsupported storage engine: InnoDB
2019-04-04 7:11:25 139734049481152 [ERROR] Aborting
里面有关键的一句:
InnoDB: Cannot allocate memory for the buffer pool
这句话报错的意思是内存不够了,要么增加内存,要么减小buffer pool的大小。
我选择减小buffer pool 的大小,在[mysqld]下面增加一行:
nano /etc/mysql/mariadb.conf.d/50-server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
innodb_buffer_pool_size = 32M
重新启动MariaDB:
service mysql start