my20_mysql的本地用户无法连接到数据库

mysql的本地用户无法连接到数据库
$ mysql -uadmin -prootroot -hlocalhost -P3309
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysql/mysql.sock' (2)

查看帮助
mysql --help

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
The following groups are read: mysql client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.

/etc/my.cnf 里面果然有下面一行,mysql默认读了/etc/my.cnf,可该实例的配置文件并不是/etc/my.cnf,所以就出错了
socket = /data/mysql/mysql.sock

配置文件寻找顺序
1. 命令行中的 --defaults-file=
2. 依次 /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

虽然找到了原因,但由于该服务器是多实例,/etc/my.cnf有实例在用,尝试以下参数使用情况

加--no-defaults参数变成了读/tmp/mysql.sock

# mysql --no-defaults -uadmin -prootroot -hlocalhost -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

添加--defaults-file参数可以登录

# mysql --defaults-file=/etc/my_vodb.cnf -uadmin -prootroot -hlocalhost -P3306

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

admin@localhost Fri Oct 12 16:36:45 2018 16:36:45 [(none)]>

使用127.0.0.1却可以直接登录

# mysql -uadmin -prootroot -h127.0.0.1 -P3306

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

mysql>

另外以下方式也是可以登录的

mysql --no-defaults -uadmin -pu5EvNtomhXdvr23 -h127.0.0.1 -P3309

mysql --defaults-file=/etc/my_tanpf.cnf -uadmin -pu5EvNtomhXdvr23 -h127.0.0.1 -P3309

除了以上原因外,还有一种情况就是密码错了,就算你非常确定密码没有错,不防更新一下密码,再连接一下试试

alter user `admin`@`127.0.0.1` identified by '原来的密码';

当然加-S参数也是可以的,这里就是找一找为什么有时会连接不上的原因

mysql -uadmin -prootroot -S /data/mysql/log/test/mysql_test.sock

如果出现了127.0.0.1可以登录,但localhost却不可以登录的情况,确保存在localhost用户的情况下,应该是网络解析或mysql内部的问题,所以,创建本地用户,优先使用127.0.0.1

原文地址:https://www.cnblogs.com/perfei/p/9778757.html