PR

mysqlコマンド上からOSコマンドを実行する方法

スポンサーリンク
Linux

mysqlコマンド上から、通常だとSQLを発行すると思いますが、OSコマンドを実行する方法があるので紹介します。

mysqlコマンドでMySQLに接続

[dekien@cent77 ]$ mysql -u dekien -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

OSコマンドを実行

mysqlプロンプト上からsystemに続けて実行したいOSコマンドを指定します

mysql> system ls -l /usr/local
合計 48
drwxr-xr-x  2 root root 4096 10月 22 04:53 2018 arc
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 bin
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 etc
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 games
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 include
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 lib
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 lib64
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 libexec
drwxr-xr-x  4 root root 4096 10月 22 04:54 2018 percona
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 sbin
drwxr-xr-x. 5 root root 4096 10月 19 05:59 2018 share
drwxr-xr-x. 2 root root 4096  9月 23 20:50 2011 src

実行されるOSコマンドは自サーバでの結果となります。

-hオプション等でリモートでMySQLサーバに接続しても、リモート先ではOSコマンドは発行できないことに注意してください。

(セキュリティ上当たり前なことなのですが)