1、source语法如下Usage:mysql>. | source 举例mysql>. /usr/local/mysql/aa.sqlQuery 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.sqlERROR:Failed to open file 'aa.sql', error: 2查看当前登陆位置:mysql> system pwd;/homemysql>把aa.sql文件放在home目录下,再次执行source指令:mysql> . aa.sqlQuery 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 -pEnter password:Welcome to the MySQL mands end with ; or g.Your MySQL connection id is 250Server version: 5.5.37-log Source distributionCopyright (c) 2000, , Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> use rgf;Database changedmysql> 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.sqlQuery OK, 0 rows affected (0.01 sec)(b)使用.时,语句结尾不能使用分号;mysql> . aa.sqlQuery 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: 25、小结source执行的当前目录即为启动mysql登陆时所处的位置。如果不想直接输入一大串目录执行sql文件,可以事先在指定位置创建好文件夹,在文件夹中创建脚本,并从文件夹位置登陆mysql。这也是一种处理问题的方法。另外还要注意source和.的语句结尾是否要带分号。有兴趣的伙伴可以试试。
如果觉得《mysql用法source_mysql中source用法一瞥》对你有帮助,请点赞、收藏,并留下你的观点哦!