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稍微安全一點。
大家在部署過程中如有問題可留言或關注微信公眾號溝通。
另外,大家幫忙關注一下我的微信公眾號: 數據庫干貨鋪 ,將不定期有書籍及學習資料贈送