Many friends have installed MySQL but do not know how to use it. In this article, we will connect to MySQL,
You can learn some common MySQL commands by changing passwords and adding users.
1. Connect to MySQL.
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 mysqlin, and then enter the command mysql-uroot-P
After the car prompts you to lose the password, if you have just installed MySQL, the Super User Root does not have the password, so directly back
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 name is root and the password is abcd123. Enter the following command:
Mysql-h110.110.110.110-uroot-pabcd123
(Note: you do not need to add spaces for u and root. The same applies to others)
3. Exit MySQL command: exit (Press ENTER)
2. 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
Another method:
Shell> mysql-u root-P
Mysql> set password for root = PASSWORD ("root ");
3. Add new users. (Note: Unlike the above, the following commands in the MySQL environment are followed
A semicolon is used 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
Query, insert, modify, and delete permissions. First, use the root user to connect to MySQL, and then enter the following
Order:
Grant select, insert, update, delete on *. * To test1 @ "%" identified
By "ABC ";
However, the User Added to Example 1 is very dangerous. If you want someone to know the password of test1, then he can
Log on to your MySQL database on any computer on the Internet and do whatever you want,
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 and access the database
Mydb query, insert, modify, and delete operations (localhost refers to the local host, that is, MySQL DATA
The host where the database is located.
Access the database only through the web page 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 "";
Database Operations. Note: you must first log on to MySQL.
And each command ends with a semicolon.
I. Operation Skills
1. If you forget the extra points when you press enter, you do not need to repeat the command. You only need to enter a semicolon to return
The car is enough. That is to say, you can divide a complete command into several lines and end it with a semicolon.
The flag is OK.
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 a mysql-3.23.27-beta-win.
Ii. Display commands
1. display the Database List.
Show databases;
At the beginning, there were only two databases: MySQL and test. The MySQL database contains MySQL system information,
We use this database to change the password and add new users.
2. display the data tables in the database:
Use MySQL; // open the database. If you have learned FOXBASE, you will not be unfamiliar with it.
Show tables;
3. display the data table structure:
Describe table name;
4. database creation:
Create Database database name;
5. Create a table:
Use Database Name;
Create Table Name (field setting list );
6. Delete databases and tables:
Drop database database name;
Drop table name;
7. Clear records in the table:
Delete from table name;
8. Display records in the table:
Select * from table name;
3. An instance for creating a database, creating a table, and inserting data
Drop database if exists school; // Delete if school exists
Create Database school; // create a database School
Use school; // open the school library
Create Table teacher // create table 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 values ('', 'glengang ', 'shenzhen Zhongyi', '2017-10-10 ');
Insert into teacher values ('', 'jack', 'shenzhen Zhongyi ', '2017-12-23 ');
Note: Table Creation
(1) set the ID to a numeric field with a length of 3: int (3), and automatically add auto_increment to each record,
Cannot be blank: not null, and make it the primary key.
(2) Set name to a 10-character field
(3) set address to a 50-character field, and the default value is Shenzhen. What is the difference between varchar and Char?
It will only be said later.
(4) set year as the date field.
If you type the preceding command at the MySQL prompt, debugging is not convenient. You can run the preceding command
It is written as is to a text file, assuming it is school. SQL, then copied to C:, and enter the directory in DOS status
Mysqlin, and then type the following command:
Mysql-uroot-P password <C: School. SQL
If it succeeds, no display is displayed for a blank row. If there is an error, a prompt is displayed. (The above command has been debugged, you
You only need to remove the // annotation ).
4. Transfer text data to the database
1. Text data should conform to the format: field data is separated by the tab key, and null value is used instead. Example:
3 ROSE:
4 MIKE: Shenzhen No. 1,-12-23
2. Data Import command load data local infile "file name" into Table table name;
Note: You 'd better copy the file to the mysqlin directory and use the use command to select the database where the table is located.
5. export and import data: (the command is executed in the DOS mysqlin directory)
Export table
Mysqldump -- opt school> school. SQL
Note: Back up all the tables in the school database to the school. SQL file. School. SQL is a text file,
Select the file name. open the file to see if you have any new discoveries.
Mysqldump -- opt School Teacher Student> school. Teacher. Student. SQL
Note: Back up the teacher and student tables in the school database to school. Teacher. Student. SQL.
.
Import table
MySQL
Mysql> Create Database School;
Mysql> use school;
Mysql> source school. SQL;
(Or change school. SQL to school. Teacher. SQL/school. Teacher. Student. SQL)
Export Database
Mysqldump -- databases db1 DB2> db1.db2. SQL
Note: Back up database DBL and DB2 to the db1.db2. SQL file. db1.db2. SQL is a text file with a file name.
Choose, open it, and you will see new discoveries.
(For example:
Mysqldump-H host-u user-P pass -- databases dbname> file. Dump
Import the database dbname on the host named "user" and "password pass" to file. Dump .)
Import Database
Mysql <db1.db2. SQL
Copy Database
Mysqldump -- all-databases> all-databases. SQL
Note: backing up all databases to a all-databases. SQL file, a all-databases. SQL is a text file,
File Name.
Import Database
MySQL
Mysql> drop database;
Mysql> drop database B;
Mysql> drop database c;
...
Mysql> source all-databases. SQL; (or exit MySQL after exiting MySQL mysql <all-databases. SQL)
Postscript:
In fact, MySQL's database operations are similar to those of other SQL databases. You 'd better read this SQL book.
See. Here I will only introduce some basic things. In fact, I only understand these things.
The best MySQL tutorial is the MySQL Chinese reference manual translated by Yan Zi ". Not only is it free, but every related website has
Download, and it is the most authoritative. Unfortunately, unlike the "PhP4 Chinese manual", it is in chm format.
Time is not convenient.
3. Open the database: Use dbname;
Show all databases: Show databases;
Display All Tables in MySQL: use MySQL first, and then show tables;
Displays the table column information: Describe user; (displays the user table information in the MySQL database );
4. Create a full Super User that can connect to the server from any address, but you must use a password something to do this
Grant all privileges on *. * to Monty @ localhost identified by 'something' with grant option;
Grant all privileges on *. * to Monty @ "%" identified by 'something' with grant option;
5. Delete authorization:
Revoke all privileges on *. * from root @ "% ";
Use MySQL;
Delete from user where user = "root" and host = "% ";
Flush privileges;
6. Create a User custom to log on to weiqiong.com on a specific client and access the specific database bankaccount.
Mysql> grant select, insert, update, delete, create, drop on bankaccount .*
To [email] custom@weiqiong.com [/Email] identified by 'stupid ';
7. Rename the table:
Alter table T1 rename T2;
To change column A from integer to tinyint not null (same name ),
Change Column B from Char (10) To char (20), rename it, and change from B to C:
Alter table T2 modify a tinyint not null, change B C char (20 );
Add a new timestamp column named D:
Alter table T2 add D timestamp;
Add an index on column D and set column A as the primary key:
Alter table T2 add index (d), add primary key ();
Delete column C:
Alter table T2 drop column C;
Add a new auto_increment integer column named C:
Alter table T2 add c int unsigned not null auto_increment, add index (C );
Note that we have indexed C, because the auto_increment column must be indexed, and we declare C as not null,
Because the indexed Column cannot be null.
8. Delete record:
Delete from T1 where c> 10;
6. Change several rows:
Update T1 set user = weiqiong, password = weiqiong;
7. Create an index with the first 10 characters of the name column:
Create index part_of_name on customer (name (10 ));
Data Import problems (by Sese and zjtv)
You must have used the Database Import and Export function in phpMyAdmin, which is very convenient. However, in practical application, I found the following problems:
1. The size of the database exceeds a certain size. For example, if the size of 6 MB is used for export, the database can be correctly saved to the local hard disk, but the import will not work! The reason is: the size of temporary files/uploaded files is limited to 2 MB in PHP. ini, while phpMyAdmin uses the upload method, resulting in a failure.
2. When importing the. SQL file exported to the hard disk back, the import may fail due to some single quotation marks, which can only be imported using applications such as MySQL.
My database has exceeded 10 MB, so this problem must be solved. My ideas:
Export: Use phpMyAdmin to save the database/table structure, use scripts to read the database content, and save it to a file!
Import: Use phpMyAdmin to restore the database/table structure, use scripts to read files, and save them to the database!
The Export program is as follows: The call method is ***. php? Table = tablename
This simple program currently saves a table at a time !! Data of one field for each behavior !!
If ($ table = "") Exit ();
Mysql_connect ("localhost", "name", "password ");
Mysql_select_db ("Database ");
$ Result = mysql_query ("select * from $ table ");
If (mysql_num_rows ($ result) <= 0) Exit ();
Echo "starts to convert data to text...
";
$ Handle = fopen ("Export table.txt", "W ");
$ Numfields = mysql_num_fields ($ result );
Fputs ($ handle, $ numfields. "\ r \ n ");
For ($ k = 0; $ K
{
$ MSG = mysql_fetch_row ($ result );
For ($ I = 0; $ I <$ numfields; $ I ++)
{
$ MSG [$ I] = str_replace ("\ r \ n", "& php2000mysqlreturn &", $ MSG [$ I]);
$ MSG [$ I] = str_replace ("\ n", "& php2000mysqlreturn &", $ MSG [$ I]);
Fputs ($ handle, $ MSG [$ I]. "\ r \ n ");
}
Fputs ($ handle, "------- php2000 dump data program V1.0 for MySQL -------- \ r \ n ");
}
Fclose ($ handle );
Echo "OK ";
?>
The imported program is as follows: the usage is the same as above!
If ($ table = "") Exit ();
Mysql_connect ("localhost", "name", "password ");
Mysql_select_db ("Database ");
$ Message = file ("cmdtable.txt ");
Echo $ numfields = chop ($ message [0]);
For ($ k = 1; $ K
{
$ Value = "";
For ($ I = $ K; $ I <($ K + $ numfields-1); $ I ++)
{
$ TMP = str_replace ("& php2000mysqlreturn &", "\ r \ n", chop ($ message [$ I]);
$ Value. = "'". addslashes ($ TMP )."',";
}
$ TMP = str_replace ("& php2000mysqlreturn &", "\ r \ n", chop ($ message [$ K + $ numfields-1]);
$ Value. = "'". $ TMP ."'";
$ Query = "insert into $ table values (". $ value .")";
Echo mysql_error ();
Mysql_query ($ query );
Echo $ K ."";
}
Echo "OK ";
?>
Usage and possible problems!
1. There may be problems with the file () function during import (my 10 m data is normal). You can change it to fopen () and then read a row again !!
2. FTP is required for import and export. After export, FTP is used to transfer the data to the local machine. FTP is used to transfer the data to the server before import!
The above shows how to import big data in phpMyAdmin.
The method below can be used for less than 2 MB
Backup: After logging on to phpMyAdmin of the space, select the database to be backed up and imported on the left side, and select the export tag at the top of the data table list on the right side, which is the one after the SQL, on the left side, select the table to be backed up and save the preceding table as a file. Click the following to execute the command and the download will jump out.
Import:
Go to the phpMyAdmin Control Panel
Select Chinese display
Select the database you want to import on the left. Click
Click SQL at the top of the page
You can import data on the redirected page. The import method is similar to that of uploading files.
You can also import
As you know, the. MySQL management tool phpMyAdmin is difficult to directly import data larger than 2 MB.
Here is a good database backup tool:
Who needs to download the mysql_tool database backup tool developed by IPB?
[Url] http://www.verymm.com/soft/php/mysql_tool.rar#/url]
I customized a MySQL database backup tool.
Easy to use
If you are interested, you can recommend it to your friends.
Haha
Applicable to scenarios where you do not have permission to manage backups in a space
Features:
Generate an entire SQL backup file on the server
Only SQL files can be imported during import, not necessarily files backed up by this tool.
An error occurs during import. If you skip this step, you can import the data in a segment.
Unlike commands, errors cannot be imported.
Haha
I have imported a 78 m SQL independent file, about 20 minutes
Although it is not very fast, at least the error will not cause too many problems.