不重复!寻找一些功能有phpmyadmin在命令行导出
我想从命令行导出和导入一个.sql文件到MySQL数据库。
MySQL中有导出。sql文件的命令吗?那我怎么导入呢?
在进行导出/导入时,可能会有一些限制,比如启用/禁用外键检查或只导出表结构。
我们可以用mysqldump设置这些选项吗?
选项的一些例子
不重复!寻找一些功能有phpmyadmin在命令行导出
我想从命令行导出和导入一个.sql文件到MySQL数据库。
MySQL中有导出。sql文件的命令吗?那我怎么导入呢?
在进行导出/导入时,可能会有一些限制,比如启用/禁用外键检查或只导出表结构。
我们可以用mysqldump设置这些选项吗?
选项的一些例子
当前回答
如果你已经在运行SQL shell,你可以使用source命令导入数据:
use databasename;
source data.sql;
其他回答
如果你已经在运行SQL shell,你可以使用source命令导入数据:
use databasename;
source data.sql;
输入以下命令导入sql数据文件:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
在本例中,导入'data。Sql '文件到'博客'数据库使用vivek作为用户名:
$ mysql -u vivek -p -h localhost blog < data.sql
如果您有专用的数据库服务器,请将localhost hostname替换为实际的服务器名称或IP地址,如下所示:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
使用以下命令导出数据库:
mysqldump -u username -p databasename > filename.sql
注意每种情况下的<和>符号。
试一试
mysqldump databaseExample > file.sql
您可以使用此脚本从终端导出或导入任何数据库,链接如下:https://github.com/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平台使用'|'节省磁盘空间。
@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时不存在
# --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:写入时错误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
第32730行错误1419 (HY000):您没有SUPER权限并且启用了二进制日志记录(您可能希望使用不太安全的log_bin_trust_function_creators变量)
# set SET GLOBAL log_bin_trust_function_creators = 1;
# or use super user import data
第138行错误1227(42000):访问被拒绝;您需要(至少一个)SUPER特权来执行此操作 mysqldump:写入时错误32
# add sed/awk to avoid some privilege issues
希望这对你有所帮助!