如何从终端循环,没有过多的密码请求 [英] how do i loop from terminal without excessive password requests
问题描述
当从 linux
( CentOS 7 $)运行以下
mysql
c $ c>) terminal
,以下命令似乎要求循环中的每个表的密码。有500多个表。我输入密码500+次是不合理的。如何修复下面的代码,使它只需要密码几次?
When running the following mysql
code from a linux
(CentOS 7
) terminal
, the following command appears to be asking for the password for EVERY table in the loop. There are 500+ tables. It is not reasonable for me to type in a password 500+ times. How can I fix the code below so that it only asks for the password a couple times?
mysql -u root -p -Nse 'show tables' DATABASE_NAME | while read table; do mysql -u root -p -e "SET FOREIGN_KEY_CHECKS = 0; truncate table $table" DATABASE_NAME; done;
编辑:
有没有办法这样做而不必在命令行日志中输入密码?
Is there a way to do this without having to put the password in the command line logs?
推荐答案
写这个,所以它只使用两个数据库会话。一个产生要执行到文件中的语句的列表,第二个(单个)来处理语句。
Consider re-writing this, so that it only uses two database sessions. One to produce a list of statements to execute into a file, and second (single) to process the statements. There's potential for something to go wrong with the first query session, so I'd be careful to separate them into two tasks.
在第一个会话中,禁止格式化,因为第一个查询会话可能会出现错误,所以我会小心将它们分成两个任务。并将stdout重定向到文件
In the first session, suppress the formatting, and redirect stdout to a file
SELECT 'set foreign_key_checks = 0;' AS stmt
SELECT CONCAT('TRUNCATE TABLE `',t.table_schema,'`.`',table_name,'`;') AS stmt
FROM information_schema.tables t
WHERE t.table_schema = 'mydatabase'
ORDER BY t.table_name ;
验证该文件是否包含您想要的内容。
Verify that the file contains what you want.
然后(另一个无用的cat)管道文件的内容到mysql
Then (another useless use of cat) pipe the contents of that file into mysql
cat myfile | mysql -u me -p --database mydatabase
这篇关于如何从终端循环,没有过多的密码请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!