1. ホーム
  2. スクリプト・コラム
  3. DOS/BAT(ドス・バット

WindowsでのMysql sqlステートメントのバッチ実行

2022-01-01 21:18:59

時々、定期的にmysqlを実行するためにbatを使用する必要がある場合、我々は、次のコードを参照することができます。

 コードに直行します。

@ECHO OFF 

SET dbhost=hostname (e.g. 127.0.0.1)
SET dbuser=username (example: root)
SET dbpasswd=user password (example: root)
SET dbName=database name (example: test)
SET sqlpath=%~dp0 (this statement ensures that the .sql file in the .bat sibling directory can be found)
set sqlfile=update.sql (you need to execute the file to execute sql)

Enter the bin directory of the mysql installation path, if the environment variables are configured then this statement is not needed

(For example: cd /d C:\Program Files\MySQL\MySQL Server 5.5\bin)

::Execute the SQL script

mysql -h%dbhost% -u%dbuser% -p%dbpasswd% < %sqlpath%%sqlfile% --default-character-set=utf8

ECHO Done!
PAUSE

@ECHO Done! 

WindowsでSQLファイルを実行する - SQL文の一括実行

以下の内容で新規にtxtファイルを作成し、.cmdファイルに変更し、実行をクリックします。

rem MySQL_HOME The local MySQL installation path
rem host the ip address of the mysql server, either locally or remotely
rem port mysql server port, default is 3306
rem user password User name and password with permission to operate the database, such as root
rem default-character-set The character set used by the database
rem database The name of the data to connect to, test is used here
rem test.sql The script file to execute, here is test.sql
rem mysql The following should be put on one line.
set MySQL_HOME=C:\mysql-4.1.10-win32
set PATH=%MySQL_HOME%\bin;%PATH%

mysql --host=192.168.0.66 --port=3306 --user=root --password=123 --default-character-set=utf8 test<test.sql


WindowsでのMySQLスクリプトファイルの一括実行

I.

@echo off
Setlocal enabledelayedexpansion
::CODER BY Mark_Li POWERD BY iBAT 1.6
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
:: Database name
@set db=hrms
:: Username
@set userName=root
:: Password
@set password=
:: sql script to execute
@set sqlpath="C:\Program Files\MySQL\MySQL Server 5.5\test_hrms.sql"
:: connect to MySQL database and execute sql script -f script execution continues with an error --default-character-set specifies the encoding of the imported data (same as the database encoding)
mysql -f -u %userName% --password=%password% %db% < %sqlpath% --default-character-set=gbk
:: Do not close the dos window immediately after execution

pause

次に、タイムアウトコマンドを設定します

my.iniファイルの下に、以下を追加します。

wait_timeout=2880000
対話型タイムアウト = 2880000
max_allowed_packet = 100M

III. 複数のSQLスクリプトを実行したい場合

test_hrms.sqlをソースに置き換える "C:\Program FilesMySQLMySQL Server 5.5test_hrms.sql"C:\Program FilesMySQLMySQL Server 5.5test_hrms2.sql"

"C:♪Program FilesMySQL ♪MySQL Server 5.5test_hrms3.sql".C:♪Program FilesMySQL ♪MySQL Server 5.5test_hrms3.sql"

WindowsでMysqlのsql文をbatで実行する方法についての記事は以上です。bat executing Mysql sql contentについては、スクリプトハウスの過去記事を検索するか、以下の関連記事を引き続き閲覧してください。