mysql怎么⽤命令⾏导出sql⽂件_MySQL:如何从命令⾏导出
和导⼊.sql⽂件?
要导出
如果是整个数据库,那么:
$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql
如果是全部数据库,那么:
$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql
如果它是DB中的特定表,则:
$ mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql
你甚⾄可以使⽤gzip⾃动压缩输出(如果你的数据库⾮常⼤):
$ mysqldump -u [uname] -p[pass] db_name | gzip > db_
如果您想远程执⾏此操作,并且有权访问相关服务器,则可以使⽤以下命令(假定MySQL服务器位于端⼝3306上):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql
导⼊
键⼊以下命令导⼊sql数据⽂件:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
在这个例⼦中,使⽤sat作为⽤户名将'data.sql'⽂件导⼊'blog'数据库:
$ mysql -u sat -p -h localhost blog < data.sql
如果您有专⽤的数据库服务器,请使⽤实际的服务器名称或IP地址replacelocalhost主机名,如下所⽰:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
或者使⽤主机名,例如berciti.biz
$ mysql -u username -p -berciti.biz database-name < data.sql
如果您不知道数据库名称或数据库名称包含在SQL转储,您可以尝试如下所⽰:
$ mysql -u username -p -h 202.54.1.10 < data.sql
请参阅: http : //sql/doc/refman/5.6/en/mysqldump.html
如果你想要⼀个GUI⼯具,那么你可以使⽤SQLyog
键⼊以下命令导⼊sql数据⽂件:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
在这个例⼦中,使⽤vivek作为⽤户名将'data.sql'⽂件导⼊'blog'数据库:
$ mysql -u vivek -p -h localhost blog < data.sql
如果您有专⽤的数据库服务器,请使⽤实际的服务器名称或IP地址replacelocalhost主机名,如下所⽰:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
要导出数据库,请使⽤以下命令:
mysqldump -u username -p databasename > filename.sql
请注意每种情况下的符号。
尝试
mysqldump databaseExample > file.sql
如果您已经在运⾏SQL shell,则可以使⽤source命令导⼊数据:
use databasename; source data.sql;
mysqldump不会转储数据库事件,触发器和例程,除⾮在转储个别数据库时明确指出;
mysqldump -uuser -p db_name --events --triggers --routines > db_name.sql
将整个数据库转储到⽂件中:
mysqldump -u USERNAME -p password DATABASENAME > FILENAME.sql
那么你可以使⽤下⾯的命令导出,
mysqldump –database –user = root –password your_db_name> export_into_db.sql
并且⽣成的⽂件将在您运⾏此命令的同⼀⽬录中可⽤。
现在⽤命令login到mysql,
mysql -u [⽤户名] -p
然后在⽂件path中使⽤“source”命令。
你可以find更多的: 导⼊导出MySQL数据库
请享⽤ :)
您可以使⽤此脚本导出或导⼊从此链接提供的任何数据库: https :
//github/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh
echo -e "Welcome to the import/export database utility\n" echo -e "the default location of mysqldump file is:
/opt/lampp/bin/mysqldump\n" echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n" read -p 'Would like you like to change the default location [y/n]: ' location_change read -p "Please enter your username: " u_name read -p
'Would you like to import or export a database: [import/export]: ' action echo
mysqldump_location=/opt/lampp/bin/mysqldump mysql_location=/opt/lampp/bin/mysql if [ "$action" == "export" ]; then if [ "$location_change" == "y" ]; then read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location echo else echo -e "Using default location of mysqldump\n" fi read -p 'Give the name of database in which you would like to export: ' db_name read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file $mysqldump_location -u $u_name -p $db_name > $sql_file elif [ "$action" == "import" ]; then if [ "$location_change" == "y" ]; then read -p 'Give the location of mysql that you want to use: ' mysql_location echo else echo -e "Using default location of mysql\n" fi read -p 'Give the complete path of the .sql file you would like to import: ' sql_file read -p 'Give the name of database in which to import this file: ' db_name $mysql_location -u $u_name -p $db_name < $sql_file else echo "please select a valid command" fi
因为我没有⾜够的声望来评论最⾼职位,所以我在这⾥添加。
使⽤'|' 在linux平台上节省磁盘空间。
thx @Hariboo,添加事件/触发器/路由参数
mysqldump -x -u [uname] -p[pass] -C --databases db_name --events --triggers --routines | sed -e 's/DEFINER[ ]*=[ ]*
[^*]*\*/\*/ ' | awk '{ if (index($0,"GTID_PURGED")) { getline; while (length($0) > 0) { getline; } } else { print $0 } }' | grep -iv 'set @@' | trickle -u 10240 mysql -u username -p -h localhost DATA-BASE-NAME
⼀些问题/提⽰:
错误:……在使⽤LOCK TABLES时不存在下载mysql服务端命令
# --lock-all-tables,-x , this parameter is to keep data consistency because some transaction may still be working like schedule. # also you need check and confirm: grant all privileges on *.* to root@"%" identified by "Passwd";
错误2006(HY000)在866⾏:MySQL服务器已经消失mysqldump:写到errno 32
# set this values big enough on destination mysql server, like: max_allowed_packet=1024*1024*20 # use compress parameter '-C' # use trickle to limit network bandwidth while write data to destination server
错误1419(HY000)在⾏32730:您没有SUPER权限和启⽤⼆进制⽇志logging(您可能希望使⽤不太安全的
log_bin_trust_function_creatorsvariables)
# set SET GLOBAL log_bin_trust_function_creators = 1; # or use super user import data
错误1227(42000)在⾏138:访问被拒绝; 你需要(⾄less有⼀个)SUPER权限(s)⽤于这个操作mysqldump:在写⼊时得到errno 32
# add sed/awk to avoid some privilege issues
希望这个帮助!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论