首先要確保數據庫存在,如果不存在則創建
方法1 source
很智能,很方便,很快捷。
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 54467
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2018, 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 wenbo
Database changed
mysql> source ./wenbo_test.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.04 sec)
Query OK, 0 rows affected (0.00 sec)
你可以執行很多次都沒有關系。
可以執行數據庫,可以執行單個表,都是可以的。
mysql> source ./wenbo_online.sql
如果sql中是整個數據庫,那么source之后,整個數據庫都變了。如果只是整個表的數據,那么整個表的數據都變了。
其是它就相當於,將sql文件中的sql語句全部執行一遍。具體什么效果,還要看sql語句的內容。
方法2 <
mysql -uroot -p wenbo < ./wenbo_test.sql
效果跟source一樣,只是沒有執行過程。
tips:導出數據用mysqldump
mysqldump -uroot -p wenbo > ./wenbo_test.sql