The mysql_config_editor utility enables you to store authentication credentials in an obfuscated login path file named .mylogin.cnf
. The file location is the %APPDATA%\MySQL
directory on Windows and the current user's home directory on non-Windows systems. The file can be read later by MySQL client programs to obtain authentication credentials for connecting to MySQL Server.html
The unobfuscated format of the .mylogin.cnf
login path file consists of option groups, similar to other option files. Each option group in .mylogin.cnf
is called a 「login path,」 which is a group that permits only certain options: host
, user
, password
, port
and socket
. Think of a login path option group as a set of options that specify which MySQL server to connect to and which account to authenticate as. Here is an unobfuscated example: (該工具的做用就是將mysql的登陸憑證寫到一個默認的.mylogin.cnf配置文件裏,而後在執行命令mysql --login-path=mypath時,會使用該配置文件裏的憑證自動登陸)mysql
[client]
user = mydefaultname
password = mydefaultpass
host = 127.0.0.1
[mypath]
user = myothername
password = myotherpass
host = localhost
1. 設置MySQL免密碼登陸
mysql_config_editor set --login-path=testlogin --user=root --host=localhost --password
執行上面的命令後,mysql 會提示你輸入root的密碼。
該命令會在root的home目錄下建立一個.mylogin.cnf配置文件,內容爲加密狀態,包含上面的mysql登陸時須要用到的賬號密碼、服務器等。
2. 建立備份腳本
# vi testbackupdb.sh
#!/bin/bash echo 'Begin Backup test DB' DATE1=`date -d "now" +%Y%m%d_%H%M%S` mysqlpump --login-path=testlogin --default-character-set=UTF8MB4 --single-transaction --compress-output=LZ4 --default-parallelism=3 -B testdb
--log-error-file=/root/db_backup/logs/testdb_bak_$DATE1.log >/root/db_backup/testdb_bak_$DATE1.sql.lz
3. 添加定時任務
# crontab -e 01 02 * * * /root/db_backup/testbackupdb.sh