I will teach you how to deal with MySQL databases easily.

Source: Internet
Author: User
Tags mysql host sql error

I. MYSQL connection format:
Mysql-h host address
-U User Name
-P User Password
1. Example 1: connect to MySQL on the local machine:
First, open the DOS window, enter the directory mysqlbin, then type the command mysql-uroot-p, and press enter to prompt you to enter the password. If you have just installed MYSQL, super User root has no password, so press enter to enter MYSQL. The MYSQL prompt is: mysql>.
2. Example 2: connect to MYSQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Run the following command: mysql-h110.110.110.110-uroot-pabcd123.
3. exit MYSQL command: exit (Press ENTER ).
Ii. Change the password
Format: mysqladmin-u username-p old password New password
1. Example 1: Add a password ab12 to the root user. First, enter the directory mysqlbin in DOS, and then type the following command:
Mysqladmin-uroot-password ab12
Note: because the root account does not have a password at the beginning, the old-p password can be omitted.
2. Example 2: Change the root password to djg345:
Mysqladmin-uroot-pab12 password djg345
3. Add new users
(Note: Unlike the above, the following commands in the MYSQL environment are followed by a semicolon as the command Terminator)
Format:
Grant select on database .*
To username @ login host identified by "password"
Example 1: Add a user named "test1" with the password "abc" so that the user can log on to any host and have the permission to query, insert, modify, and delete all databases. First, use the root user to connect to MYSQL, and then type the following command:
Grant select, insert, update,
Delete on *. * to test1 @ "%" Identified by "abc ";
However, the User Added in Example 1 is very dangerous. If someone knows the password of test1, then he can log on to your mysql database on any computer on the internet and do whatever he wants. For the solution, see Example 2.
Example 2: Add a user named "test2" with the password "abc" so that the user can only log on to localhost, you can also query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), so that the user knows the password of test2, he cannot access the database directly from the internet, but can only access the database through the web pages on the MYSQL host.
Grant select, insert, update, delete on mydb. * to test2 @ localhost identified by "abc ";
If you do not want test2 to have a password, you can run another command to remove the password.
Grant select, insert, update, delete on mydb. * to test2 @ localhost identified "";
Note: you must first log on to MYSQL. The following operations are performed at the MYSQL prompt and each command ends with a semicolon.
Iv. Operation Skills
1. If you forget the extra points after you press Enter when making the command, you don't have to repeat the command. You just need to press a semicolon to press Enter. That is to say, you can divide a complete command into several lines, and then use a semicolon as the end sign to complete the operation.
2. You can use the cursor to bring up or down the previous commands. However, an old MYSQL version I used earlier does not support this feature. I am using: mysql-3.23.27-beta-win.
5. Display commands
1. display the Database List: show databases;
At the beginning, there were only two databases: mysql and test. The mysql database contains the MYSQL system information. We change the password and add new users to use this database for operations.
2. display the data table in the database:
Use mysql;
// Open the database. If you have learned FOXBASE, you will be familiar with it.
Show tables;
3. display the structure of the data table: describe table name;
4. database creation: create database name;
5. Create a table:
Use Database Name;
Create table Name (field setting list );
6. Delete databases and tables:
Drop database name;
Drop table name;
7. Clear the records in the Table: delete from table name;
8. Display records in the Table: select * from table name;
6. An instance for creating a database, creating a table, and inserting data, drop database if exists school; // Delete the instance if SCHOOL exists.
Create database school;
// Create a database SCHOOL
Use school;
// Open the SCHOOL library
Create table teacher
// Create a table named TEACHER.
(
Id int (3) auto_increment not null primary key,
Name char (10) not null,
Address varchar (50) default Shenzhen,
Year date
); // Table creation ends
// Insert fields as follows
Insert into teacher valuess
(, Glchengang, Shenzhen No. 1 Middle School, 1976-10-10 );
Insert into teacher valuess
(, Jack, Shenzhen No. 1 Middle School, 1975-12-23 );



Note: In the table in progress (1), set the ID to a numeric field of 3: int (3) and make it automatically add one: auto_increment for each record. It cannot be blank: not null and set it as the main field primary key (2) Set NAME to the character field with a length of 10 (3) set ADDRESS to the character field with a length of 50, the default value is Shenzhen. What is the difference between varchar and char? It will only be discussed later. (4) set YEAR as the date field.
If you type the preceding command at the mysql prompt, debugging is not convenient. You can write the preceding command as is to a text file, assuming it is school. SQL, then copy it to c:, and enter the directory mysqlbin in DOS state, and then type the following command:
Mysql-uroot-p Password
If it succeeds, no display is displayed for a blank row. If there is an error, a prompt is displayed. (The preceding command has been debugged. You only need to remove the // annotation to use it ).
7. Transfer text materials to the database
1. Text data should conform to the format: field data should be separated by the tab key, and the null value should be replaced by n.
Example:
3 rose:
4 mike: Shenzhen No. 1,-12-23
2. data input command load data local infile "file name" into table name;
Note: You 'd better copy the file to the mysqlbin directory and use the use command to create the database where the table is located.
8. Back up the database:
(The command is executed in the DOS mysqlbin directory)
Mysqldump -- opt school> school. bbb
Note: Back up the database school to the school. bbb file. school. bbb is a text file with any file name. Open it and you will find new discoveries.

========================================================== ====================


The MySQL server startup command is as follows: net start mysql

The command to stop a MySQL server is as follows: net stop mysql

The MySQL server connection command is as follows:

Method 1: mysql-h host name-u user name-p User Password

Method 2: mysql -- host = host name -- user = username -- password = user password

The default host is localhost.

========================================================== ====================

I. Data Backup shortcuts

Because this method has not been verified in the official documentation, we call it a test.

Objective: To back up a mysql database TestA in the hostA host and restore it to the hostB host

Test environment:

Operating System: WinNT4.0, Mysql3.22.34, and phpMyAdmin 2.1.0

Install mysql database in hostA and create TestA Database

The mysql database is installed on the hostB machine, and there is no TestA Database

Procedure:

Start phpMyAdmin to check the database list in HostA and HostB. No TestA database exists in HostB.

Find the mysql installation directory in HostA, and find the database directory data

In my test environment, this directory is

C: mysqldata

Find the subdirectory of the corresponding database name

C: mysqldataTestA

Paste and copy the file to the Data directory of HostB, which is the same as the file in the Data directory of HostB Mysql.

Refresh the phpMyAdmin of HostB and check the database list. We can see that TestA has appeared, and the query and modification operations are normal. The Backup recovery is successful.

Test conclusion: Mysql databases can be stored in the form of files, backup, recovery as long as the corresponding file directory can be restored, no need to use other tools to back up.

Ii. Formal method (officially recommended ):

MySQL mysqldump is used for export. The basic usage is as follows:

Mysqldump [OPTIONS] database [tables]

If you do not specify any tables, the entire database will be exported.

Run mysqldump -- help to obtain the option table supported by your mysqldump version.

Note: If you run mysqldump without the -- quick or -- opt option, mysqldump loads the entire result set to the memory before the export result. If you are exporting a large database, this may be a problem.

Mysqldump supports the following options:

-- Add-locks

Add lock tables and unlock table before each TABLE is exported. (To make it faster to insert data to MySQL ).

-- Add-drop-table

Add a drop table before each create statement.

-- Allow-keywords

Names of columns allowed to be created as keywords. This is done by adding the table name before the column name.

-C, -- complete-insert

Use the complete insert Statement (with the column name ).

-C, -- compress

If both the client and server support compression, all information is compressed between the two.

-- Delayed

Use the insert delayed command to INSERT rows.

-E, -- extended-insert

Use the new multiline INSERT syntax. (A more compact and faster insert statement is provided)

-#, -- Debug [= option_string]

Tracking Program usage (for debugging ).

-- Help

Displays a help message and exits.

-- Fields-terminated-by =...

-- Fields-enclosed-by =...

-- Fields-optionally-enclosed-by =...

-- Fields-escaped-by =...

-- Fields-terminated-by =...

These options are used with-T options and have the same meaning as the load data infile clause.

Load data infile syntax.

-F, -- flush-logs

Wash Out the log files on the MySQL server before starting the export.

-F, -- force,

Even if we get an SQL error while exporting a table, continue.

-H, -- host = ..

Export data from the MySQL server on the named host. The default host is localhost.

-L, -- lock-tables.

Lock all tables for start export.

-T, -- no-create-info

Create table statement)

-D, -- no-data

No row information is written to the table. If you only want to export the structure of a table, this is very useful!

-- Opt

Same as -- quick -- add-drop-table -- add-locks -- extended-insert -- lock-tables.

You should be given the fastest export possible for reading a MySQL server.

-Pyour_pass, -- password [= your_pass]

The password used to connect to the server. If you do not specify "= your_pass", mysqldump requires a password from the terminal.

-P port_num, -- port = port_num

The TCP/IP Port used to connect to a host. (This is used to connect to a host other than localhost because it uses Unix sockets .)

-Q, -- quick

Directly export the data to stdout without buffering the query; Use mysql_use_result () to do it.

-S/path/to/socket, -- socket =/path/to/socket

The socket file used when connecting to localhost (which is the default host.

-T, -- tab = path-to-some-directory

For each given table, CREATE a table_name. SQL file that contains the SQL CREATE command and a table_name.txt file that contains data. Note: This only works when mysqldump runs on the same machine where the mysqld daemon is running .. The format of the txt file is determined by the options -- fields-xxx and -- lines -- xxx.

-U user_name, -- user = user_name

The username used by MySQL to connect to the server. The default value is your Unix login name.

-O var = option, -- set-variable var = option

Set the value of a variable. Possible variables are listed below.

-V, -- verbose

Lengthy mode. Print out more information about the program.

-V, -- version

Print the version information and exit.

-W, -- where = 'where-condition'

Only selected records are exported. Note that the quotation marks are mandatory!

"-- Where = user = 'jimf '"-wuserid> 1 ""-wuserid <1"

The most common use of mysqldump may make a backup of the entire database:

Mysqldump -- opt database> backup-file. SQL

However, it is also useful for enriching another MySQL database with information from one database:

Mysqldump -- opt database | mysql -- host = remote-ho

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.