English 中文(简体)
How do I run a script on/in a mysql database?
原标题:

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.

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

最佳回答
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)

mysql -u <myuser> -p < mydbcreate.sql
问题回答

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.

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

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

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:

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

And

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

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 . . .

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 -u{username} -p{password} {dbname} --no-data

(of course, without the curly braces)

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.

For #1, use the following menu command

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.

For #2, use the following menu command

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!





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签