mysql中source用法一瞥
1、source语法如下
Usage:
mysql>. <filename> | source <filename>
举例
mysql>. /usr/local/mysql/aa.sql
Query OK, 0 rows affected (0.01 sec)
2、直接使用source filename或者. filename,怎么去实现?
首先查看mysql基目录和数据目录:
mysql> show variables like ‘basedir‘;
+---------------+------------------+
| Variable_name | Value |
+---------------+------------------+
| basedir | /usr/local/mysql |
+---------------+------------------+
1 row in set (0.01 sec)
mysql> show variables like ‘datadir‘;
+---------------+------------------+
| Variable_name | Value |
+---------------+------------------+
| datadir | /opt/mysql/data/ |
+---------------+------------------+
1 row in set (0.00 sec)
把aa.sql文件分别放在该目录下:
mysql> . aa.sql
ERROR:
Failed to open file ‘aa.sql‘, error: 2
查看当前登陆位置:
mysql> system pwd;
/home
mysql>
把aa.sql文件放在home目录下,再次执行source指令:
mysql> . aa.sql
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+--------------------+
| Tables_in_iloveyou |
+--------------------+
| emp |
+--------------------+
1 row in set (0.00 sec)
3、完整示例
[root@gflinux3 opt]# mkdir rgf
[root@gflinux3 opt]# cd rgf
[root@gflinux3 rgf]# cp /home/aa.sql .
[root@gflinux3 rgf]# pwd
/opt/rgf
[root@gflinux3 rgf]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 250
Server version: 5.5.37-log Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> use rgf;
Database changed
mysql> source aa.sql;
Query OK, 0 rows affected (0.01 sec)
4、注意事项
(a)使用source时,文件最后使用;分号和不适用分号语法都正确
mysql> source aa.sql;
Query OK, 0 rows affected (0.01 sec)
mysql> drop table emp;
Query OK, 0 rows affected (0.00 sec)
mysql> source aa.sql
Query OK, 0 rows affected (0.01 sec)
(b)使用.时,语句结尾不能使用分号;
mysql> . aa.sql
Query OK, 0 rows affected (0.01 sec)
mysql> drop table emp;
Query OK, 0 rows affected (0.01 sec)
mysql> . aa.sql;
ERROR:
Failed to open file ‘aa.sql;‘, error: 2
5、小结
source执行的当前目录即为启动mysql登陆时所处的位置。如果不想直接输入一大串目录执行sql文件,可以事先在指定位置创建好文件夹,在文件夹中创建脚本,并从文件夹位置登陆mysql。这也是一种处理问题的方法。另外还要注意source和.的语句结尾是否要带分号。有兴趣的伙伴可以试试。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。