mysql登陆案例_本地登录多实例mysql ,默认登录数据库问题

本地登录多实例mysql ,默认登录数据库问题

本地多实例mysql 环境,有 3306和3307 两个端口的实例。

当本地登录时,以下登录方式登录进去的都是 3306 实例

[root@testdb1 ~]# mysql -uroot -pchengce243 -P3306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3306 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -P3307 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3306 |

+---------------+-------+

甚至一个不存在的端口号 9088 登录进去的依然是 3306 实例

[root@testdb1 ~]# mysql -uroot -pchengce243 -P9088 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3306 |

+---------------+-------+

要想登录到指定的端口号,必须指定 -h 参数 。

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P3306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3306 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P3307 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3307 |

+---------------+-------+

原因:如果不指定 -h 参数,只指定 -P 参数,默认就会去读取 默认的配置文件 中的 [client] 部分,也就是 /etc/my.cnf

查看 /etc/my.cnf 得知,配置的端口号就是 3306,以下验证一下结论,修改 /etc/my.cnf port 参数为 4406

[root@testdb1 ~]# mysql -uroot -pchengce243 -P3307 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 4406 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -P3306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 4406 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -P9306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 4406 |

+---------------+-------+

可以看到上面系统中并不存在的 3306端口和 9306 端口,登录进去就是修改后的 4406 端口。

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P4406 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 4406 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P3307 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3307 |

+---------------+-------+

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P3306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

[root@testdb1 ~]# mysql -uroot -pchengce243 -h127.0.0.1 -P9306 -e "show variables like 'port';"

mysql: [Warning] Using a password on the command line interface can be insecure.

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

所以为了解决本地登录时端口乱串的问题,最好指定 -h 参数,就不会发生登录到默认数据库的问题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值