通过命令行将 sql 文件导入 mysql

Import sql file into mysql via command line

我有一个奇怪的问题。我想通过命令行导入 sql 文件。命令看起来像

mysql -u root -p root pdweb-sandbox < C:\Apache24\htdocs\pdweb-sandbox\migrations/init.sql

但是如果在命令中明确输入了密码,它什么也不做。没有错误,但也没有行动。只有当密码为空时,它才会按预期工作(但我需要在命令行中输入它)。但我想在命令中输入密码。它是自动触发的脚本的一部分。密码来自 phinx.yml 配置文件。有没有办法在命令中使用密码来做到这一点?

您可以通过多种方式在脚本中执行此操作:

  1. 使用mysql导入。您可以在此处找到详细信息。 https://www.toadworld.com/platforms/mysql/w/wiki/6152.mysql-importing-with-mysqlimport

  2. 使用mysql,详细使用方法如下(复制自https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html#option_mysql_password

    --password[=password], -p[password]

    The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysql prompts for one.

    Specifying a password on the command line should be considered insecure. See Section 6.1.2.1, “End-User Guidelines for Password Security”. You can use an option file to avoid giving the password on the command line.

  1. 以上两种方式都不安全。您可以将密码放在另一个选项文件中。详情在这里:https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html `> 将您的密码存储在一个选项文件中。例如,在 Unix 上,您可以在主目录中 .my.cnf 文件的 [client] 部分列出您的密码:

    [client] password=your_pass To keep the password safe, the file should not be accessible to anyone but yourself. To ensure this, set the file access mode to 400 or 600. For example:

    shell> chmod 600 .my.cnf To name from the command line a specific option file containing the password, use the --defaults-file=file_name option, where file_name is the full path name to the file. For example:

    shell> mysql --defaults-file=/home/francis/mysql-opts Section 4.2.6, “Using Option Files”, discusses option files in more detail.`

  2. 使用期望。 "man expect"。 http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts