MySQL连接方式小结

1.   连接方式

1.1  方式1

 /usr/local/mysql5.7/bin/mysql  -p

此方法默认采用root@localhost用户登录,

 

1.2  方式2

/usr/local/mysql5.7/bin/mysql  -uroot -p -S /app/data/mysql3307/tmp/mysql.sock

 

1.3  方式3

/usr/local/mysql5.7/bin/mysql  -uroot -p -h 127.0.0.1 -P3307

此方式的用户和方式2的不同,如下

 

 root@localhost 和root@'127.0.0.1'是不同的用户

 

1.4 方式4

 /usr/local/mysql5.7/bin/mysql  -uroot -p -h localhost -P3307

此方式和方法1 及方法2用户相同,如下

 

1.5  方式5

/usr/local/mysql5.7/bin/mysql  -uroot -p -h 192.168.56.77 -P3307

此方式与方式3都是使用root@'%'这个用户,但是查看用户时会所有不同,例如如果在本机操作,则显示本机的ip,如下:

如果在其他机器上查看,则ip为对应机器的IP,如下

  

 2.  免密登录的方式

2.1  修改my.cnf的方式

可以在my.cnf配置文件的[client]标签下增加用户信息处理。但是,该方式默认使用的/etc/my.cnf配置文件下的信息,因此需要调整该目录下的对应标签下的信息

vim  /etc/my.cnf
/**  添加如下信息 */
[client]
user="root"
password="123456"

此时登录,无需输入密码

 

 且 mysqladmin命令也无需输入密码,如下

/usr/local/mysql5.7/bin/mysqladmin  -uroot   -S /app/data/mysql3307/tmp/mysql.sock ping
mysqld is alive

如果只想指定的命令免密,则可以在对应的标签下添加用户密码信息,例如

 vim /etc/my.cnf 
/**   注释掉[client]下的信息,只添加[mysql]标签  */
[root@mda177 mysql3307]# vim /etc/my.cnf 

#[client]
#user="root"
#password="123456"

[mysql]
user="root"
password="123456"

/**  使用mysql进行测试 */

 /usr/local/mysql5.7/bin/mysql  -uroot   -S /app/data/mysql3307/tmp/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.25-28-log Percona Server (GPL), Release 28, Revision c335905

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> exit
Bye


/**  使用mysqladmin用户进行测试 */
 /usr/local/mysql5.7/bin/mysqladmin  -uroot   -S /app/data/mysql3307/tmp/mysql.sock ping
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

以上即代表mysql命令可免密登录,而mysqladmin不能免密登录。  如果只想mysqladmin免密登录,则在[mysqladmin] 标签下添加信息即可,想所有的都可以免密就在[client]标签下添加,具体的就不再演示了。

2.2   添加在login_path

使用mysql_config_editor 命令添加需要免密登录的用户,如下

/**  添加用户 */ 
/usr/local/mysql5.7/bin/mysql_config_editor  set -G user1  -u root  -p  -S /app/data/mysql3307/tmp/mysql.sock 
Enter password: 

/**  查看添加的用户信息 */
/usr/local/mysql5.7/bin/mysql_config_editor  print --all
[user1]
user = root
password = *****                       
socket = /app/data/mysql3307/tmp/mysql.sock

可以发现,密码做了加密,非明文显式。

登录login-path指定

/**  指定login-path 登录 */
/usr/local/mysql5.7/bin/mysql  --login-path="user1"

 

 注意: 

如果密码中含有字母、数字、下划线外的字符,mysql_config_editor set最后输入密码时添加上引号。

配置后的login-path存在 ~/.mylogin.cnf文件里,此文件为二进制文件。

 ll -ah ~/.mylogin.cnf 
-rw------- 1 root root 168 Sep 10 08:46 /root/.mylogin.cnf

此方式相对于配置my.cnf稍微安全一点。

 

大家在部署过程中如有问题可留言或关注微信公众号沟通。

另外,大家帮忙关注一下我的微信公众号:   数据库干货铺  ,将不定期有书籍及学习资料赠送

 

posted @ 2019-09-10 09:27  耿小厨  阅读(5340)  评论(0编辑  收藏  举报
微信公众号: 数据库干货铺