Mac - Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

时间:2021-11-09 19:44:31

在终端输入mysql,结果出现

macdeMacBook-Pro:~ mac$ alias mysql=/usr/local/mysql/bin/mysql
macdeMacBook-Pro:~ mac$ mysql
ERROR (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

在网上搜到很多方法,不好使,然后查询了官方文档

http://dev.mysql.com/doc/refman/5.7/en/can-not-connect-to-server.html

下面写道

Here are some reasons the Can't connect to local MySQL server error might occur:

  • mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present.

  • 。。。

没想到第一条就符合,然后我尝试输入mysqld

结果:

macdeMacBook-Pro:bin mac$ mysqld
--10T07::.997151Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--10T07::.997492Z [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
--10T07::.997557Z [Note] mysqld (mysqld 5.7.) starting as process ...
--10T07::.003105Z [Warning] Setting lower_case_table_names= because file system for /usr/local/var/mysql/ is case insensitive
--10T07::.004893Z [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
--10T07::.004919Z [Note] InnoDB: Uses event mutexes
--10T07::.004926Z [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
--10T07::.004932Z [Note] InnoDB: Compressed tables use zlib 1.2.
--10T07::.005337Z [Note] InnoDB: Number of pools:
--10T07::.005498Z [Note] InnoDB: Using CPU crc32 instructions
--10T07::.007117Z [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = , chunk size = 128M
--10T07::.018796Z [Note] InnoDB: Completed initialization of buffer pool
--10T07::.035212Z [Note] InnoDB: Highest supported file format is Barracuda.
--10T07::.051578Z [Note] InnoDB: Creating shared tablespace for temporary tables
--10T07::.051764Z [Note] InnoDB: Setting file './ibtmp1' size to MB. Physically writing the file full; Please wait ...
--10T07::.065007Z [Note] InnoDB: File './ibtmp1' size is now MB.
--10T07::.065856Z [Note] InnoDB: redo rollback segment(s) found. redo rollback segment(s) are active.
--10T07::.065868Z [Note] InnoDB: non-redo rollback segment(s) are active.
--10T07::.066135Z [Note] InnoDB: Waiting for purge to start
--10T07::.121419Z [Note] InnoDB: 5.7. started; log sequence number
--10T07::.121762Z [Note] InnoDB: Loading buffer pool(s) from /usr/local/var/mysql/ib_buffer_pool
--10T07::.122048Z [Note] Plugin 'FEDERATED' is disabled.
--10T07::.133391Z [Note] InnoDB: Buffer pool(s) load completed at ::
--10T07::.134352Z [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
--10T07::.134366Z [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
--10T07::.135842Z [Warning] CA certificate ca.pem is self signed.
--10T07::.135903Z [Note] Skipping generation of RSA key pair as key files are present in data directory.
--10T07::.142139Z [Note] Server hostname (bind-address): '127.0.0.1'; port:
--10T07::.142191Z [Note] - '127.0.0.1' resolves to '127.0.0.1';
--10T07::.142330Z [Note] Server socket created on IP: '127.0.0.1'.
--10T07::.195770Z [Note] Event Scheduler: Loaded events
--10T07::.195949Z [Note] mysqld: ready for connections.
Version: '5.7.22' socket: '/tmp/mysql.sock' port: Homebrew

等一大串错误之类的 ,最后退出了

最后,正确的方法:

macdeMacBook-Pro:~ mac$ sudo mysqld_safe
Password:
Logging to '/usr/local/var/mysql/macdeMacBook-Pro.local.err'.
--10T07::.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
--10T07::.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/macdeMacBook-Pro.local.pid ended
macdeMacBook-Pro:~ mac$ mysql
ERROR (): Access denied for user 'mac'@'localhost' (using password: NO)

然后mysqld服务就运行啦

此时,终于能够打开mysql