使用终端将 SQL 数据库导入 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17171780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:58:07  来源:igfitidea点击:

Importing a SQL db into mysql using the terminal

mysqlsqlimportterminal

提问by Doug Fir

I'm struggling to do this.

我正在努力做到这一点。

I have created a new database in the terminal called "somedb" using

我在终端中创建了一个名为“somedb”的新数据库,使用

CREATE DATABASE somedb

On my desktop I have the SQL dump downloaded from phpMyadmin: somedb.sql

在我的桌面上,我从 phpMyadmin 下载了 SQL 转储:somedb.sql

I have tried:

我试过了:

somedb < /Users/myname/Desktop/somedb.sql

Result: ERROR 1064 (42000): You have an error in your SQL syntax

结果:ERROR 1064 (42000):您的 SQL 语法有错误

mysql -u myname -p -h localhost somedb </Users/myname/Desktop/somedb.sql

Result: ERROR 1064 (42000): You have an error in your SQL syntax;

结果:ERROR 1064 (42000):您的 SQL 语法有错误;

I'm new to SQL (The purpose of importing this db is for a text book exercise)

我是 SQL 新手(导入此数据库的目的是用于教科书练习)

I have granted myself all privileges and there is no password.

我已授予自己所有权限,并且没有密码。

Any idea what I'm doing wrong?

知道我做错了什么吗?

Here is the top of the SQL dump file:

这是 SQL 转储文件的顶部:

-- phpMyAdmin SQL Dump
-- version 4.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 18, 2013 at 02:22 PM
-- Server version: 5.5.31-30.3
-- PHP Version: 5.2.17

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `somedb`
--
CREATE DATABASE IF NOT EXISTS `somedb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `somedb`;

-- --------------------------------------------------------

--
-- Table structure for table `actions`
--

CREATE TABLE IF NOT EXISTS `actions` (
  `action_id` int(11) NOT NULL AUTO_INCREMENT,
  `action` varchar(75) NOT NULL,
  `qualifiers` text NOT NULL,
  `response` varchar(75) NOT NULL,
  `response_vars` text NOT NULL,
  `active` tinyint(4) NOT NULL,
  PRIMARY KEY (`action_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Stores user defined actions triggered by certain events' AUTO_INCREMENT=3 ;

-- --------------------------------------------------------

--

回答by Doug Fir

I found an SO post here.

我在这里找到了一个 SO 帖子。

I used "source" like so:

我像这样使用“来源”:

SOURCE /Users/myname/Desktop/somedb.sql;

That worked. Great but the internet seemed to want me to use the method like so:

那奏效了。很好,但互联网似乎希望我使用这样的方法:

mysql -u username -p password databasename < filename.sql

I may post another question on when to use that second method but in the meantime I just used source from a SQL dump file

我可能会发布关于何时使用第二种方法的另一个问题,但同时我只是使用了 SQL 转储文件中的源代码

回答by frshjb373

Using MAMP Pro, created "uploads" dir in MAMP and put my SQL file in there called "file.sql". Ran the query below in terminal and worked for me.

使用 MAMP Pro,在 MAMP 中创建“上传”目录并将我的 SQL 文件放在那里,称为“file.sql”。在终端中运行下面的查询并为我工作。

Make sure to replace brackets and and user info with no spaces after "-u" or "-p"

确保在“-u”或“-p”后不加空格替换方括号和用户信息

/Applications/MAMP/Library/bin/mysql -u<username> -p<root> <db_name> < /Applications/MAMP/uploads/file.sql

回答by Jaknap Ramuk

You can try this way..

你可以试试这个方法。。

Go to the mysql prompt and then type the following.

转到 mysql 提示符,然后键入以下内容。

mysql> \. <path> /filename.sql

Note the gap between . and the path of the sql file.Hope this works.

注意 之间的差距。和 sql 文件的路径。希望这有效。

回答by Khan Shahrukh

Type this on terminal

在终端输入这个

sudo mysql -p database_name < folder/database_dump_file.sql

it will then ask you for the mysql password

然后它会询问你的 mysql 密码

回答by Garistar

mysql -uUser -p --default-character-set=utf8 databasename < /<path to .sql>

(-p) will ask for password

(-p) 会要求输入密码