如何在 mysql 数据库上/中运行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2457198/
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 15:32:36  来源:igfitidea点击:

How do I run a script on/in a mysql database?

mysqlddlmysql-workbench

提问by reggie

I have created a MySQL database using MySQL Workbench. It has about 20 tables. I cannot figure out how to run scripts on the database.

我使用 MySQL Workbench 创建了一个 MySQL 数据库。它有大约20张桌子。我不知道如何在数据库上运行脚本。

Basically, I want to make a database creation script which will allow me to create my database on any other MySQL server.

基本上,我想制作一个数据库创建脚本,它允许我在任何其他 MySQL 服务器上创建我的数据库。

回答by mmacaulay

create database mydatabase;

grant all on mydatabase.* to 'myuser'@'%' identified by 'mypasswd';

create table mytable (
id int not null auto_increment,
myfield varchar(255) not null default ''
);

etc...

等等...

Put that in a file called mydbcreate.sql and run (from the command line)

将其放入名为 mydbcreate.sql 的文件中并运行(从命令行)

mysql -u <myuser> -p < mydbcreate.sql

回答by Patrick Desjardins

Here are some hints :

这里有一些提示:

mysqldump -u [username] -p[password] [databasename] > [backupfile.sql]

And

mysql -u [username] -p[password] [database_to_restore] < [backupfile.sql]

It does generate a file with structures and data. The second line take the data from the file to the database.

它确实生成了一个包含结构和数据的文件。第二行将数据从文件带到数据库。

There is no space between -p and the password. It would be better not to put the password in your command as this can end up in your .bash_history. If you omit the password it will be prompted for:

-p 和密码之间没有空格。最好不要在您的命令中输入密码,因为这可能会出现在您的 .bash_history 中。如果您省略密码,系统将提示您:

mysqldump -u [username] -p [databasename] > [backupfile.sql]

And

mysql -u [username] -p [database_to_restore] < [backupfile.sql]

回答by John Himmelman

Knowing the mysql command line utilities is useful & important but for performing common tasks use a database administration tool, such as phpMyAdmin, to create and manage your databases. These tools make it extremely easy to create & browse tables, create export scripts (a file with all the queries to create tables, insert data, etc...), and other great things.

了解 mysql 命令行实用程序是有用且重要的,但要执行常见任务,请使用数据库管理工具(例如 phpMyAdmin)来创建和管理数据库。这些工具使创建和浏览表、创建导出脚本(包含创建表、插入数据等的所有查询的文件)和其他很棒的事情变得非常容易。

phpMyAdmin is great, runs in and LAMP environment, is free, and easy to use.

phpMyAdmin 很棒,在 LAMP 环境中运行,免费且易于使用。

http://www.phpmyadmin.net/home_page/index.php

http://www.phpmyadmin.net/home_page/index.php

Screeny of exporting a db in phpMyAdmin (cropped shot)alt text http://www.typolight.org/tl_files/images/documentation/sql-export.jpg

在 phpMyAdmin 中导出数据库的屏幕(裁剪镜头)替代文本 http://www.typolight.org/tl_files/images/documentation/sql-export.jpg

回答by MJB

Perhaps what you need is to know that those statements are called DDL? Then you can go to any MySQL site and ask how to generate DDL? Not being snarky, just saying . . .

也许您需要知道这些语句称为 DDL?然后你可以去任何MySQL站点询问如何生成DDL?不矫情,只是说。. .

Also, you can try this:

另外,你可以试试这个:

use mysqldump from the command line with the --no-data option. Ask for help if need more info. But a simple example would be:

从命令行使用 mysqldump 和 --no-data 选项。如果需要更多信息,请寻求帮助。但一个简单的例子是:

$ mysqldump -u{username} -p{password} {dbname} --no-data

(of course, without the curly braces)

(当然,没有花括号)

回答by Peter Bailey

You have two options.

你有两个选择。

  1. Export a CREATE script that you can store and/or arbitrarily run on any MySQL server
  2. Synchronize with a specific MySQL server directly.
  1. 导出可以在任何 MySQL 服务器上存储和/或任意运行的 CREATE 脚本
  2. 直接与特定的 MySQL 服务器同步。

For #1, use the following menu command

对于#1,使用以下菜单命令

File >> Export >> Forward Engineer SQL CREATE Script...

From here, you can use a program like MySQL GUI Tools or see the other answers in this thread to import the generated SQL script from the command line.

从这里,您可以使用 MySQL GUI Tools 之类的程序或查看此线程中的其他答案以从命令行导入生成的 SQL 脚本。

For #2, use the following menu command

对于#2,使用以下菜单命令

Database >> Forward Engineer...
- or -
Database >> Synchronize Model...

Before you do either of these, you may want to establish a connection first via

在您执行其中任何一项之前,您可能希望首先通过以下方式建立连接

Database >> Manage Connections...

Important! For both of these options, make sure you look closely at every option available in the prompts - small settings can make a big change in the output!

重要的!对于这两个选项,请确保仔细查看提示中可用的每个选项 - 小设置可以使输出发生很大变化!