1. Connect to the database
Format: mysql -h host address -u username -p user password
1.1. Connect to MYSQL on this machine.
First open the DOS window, then enter the directory mysqlbin, and then type the command mysql -u root -p. After pressing Enter, you will be prompted to enter the password.
Note that the user name may or may not have spaces before it, but there must be no spaces before the password, otherwise it will be You re-enter your password.
If MYSQL has just been installed, the super user root does not have a password, so just press Enter to enter MYSQL. The MYSQL prompt is: mysql>
1.2 Connect to MYSQL on the remote host.
Assume that the IP of the remote host is: 110.110.110.110, the user name is root, and the password is abcd123. Then type the following command:
mysql -h110.110.110.110 -u root -p 123; (Note: There is no need to add a space between u and root, and the same is true for others)
1.3 Exit the MYSQL command: exit (Enter)
2. Add a user
Format: grant select on database.* to username@login host identified by “password”
2.1 Add a user test1 with password abc, so that he can log in on any host and query all databases. Permissions to insert, modify, and delete.
First connect to MYSQL as the root user, and then type the following command:
grant select,insert,update,delete on *.* to [email=test1@”%]test1@”%[/email]” Identified by “abc”;
But adding users is very dangerous. If someone knows the password of test1, then he can log in to any computer on the Internet. Log in to your mysql database and do whatever you want with your data. See 2.2 for the solution.
2.2 Add a user test2 with the password abc, so that he can only log in on localhost, and can query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), In this way, even if the user knows the password of test2, he cannot directly access the database from the Internet, and can only access it through the web page on the MYSQL host.
grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “abc”;
If you don’t want test2 to have a password, you can type another command to eliminate the password.
grant select,insert,update,delete on mydb.* to [email=test2@localhost]test2@localhost[/email] identified by “”
3. Operating the database
3.1 Create database
Note: You must connect to the Mysql server before creating the database
Command: create database
Example 1: Create a database named xhkdb
mysql> create database xhkdb;
Example 2 : Create a database and assign users
①CREATE DATABASE database name;
②GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON database name.* TO username@localhost IDENTIFIED BY 'password';
③SET PASSWORD FOR 'database name '@'localhost' = OLD_PASSWORD('password');
Execute 3 commands in sequence to complete the database creation.
Note: The Chinese “password” and “database” need to be set by the user themselves.
3.2 Show databases
Command: show databases (note: there is an s at the end)
mysql> show databases
3.3 Delete database
Command: drop database
For example: delete the database named xhkdb
mysql> drop database xhkdb;
Example 1: Delete a database that is sure to exist
mysql> drop database drop_database; Query OK, 0 rows affected (0.00 sec)
Example 2: Delete a database that is not sure to exist
mysql> drop database drop_database; ERROR 1008 (HY000): Can't drop database 'drop_database'; database doesn't exist //发生错误,不能删除'drop_database'数据库,该数据库不存在。 mysql> drop database if exists drop_database; Query OK, 0 rows affected, 1 warning (0.00 sec)//产生一个警告说明此数据库不存在 mysql> create database drop_database; Query OK, 1 row affected (0.00 sec) mysql> drop database if exists drop_database;//if exists 判断数据库是否存在,不存在也不产生错误 Query OK, 0 rows affected (0.00 sec)
3.4 Connect to the database
Command: use
For example: If the xhkdb database exists, try to access it: mysql> ; use xhkdb;
Screen prompt: Database changed
The use statement can notify MySQL to use the db_name database as the default (current) database for subsequent statements. This database remains the default database until the end of the segment, or until a different USE statement is issued:
mysql> USE db1; mysql> SELECT COUNT(*) FROM mytable; # selects from db1.mytable mysql> USE db2; mysql> SELECT COUNT(*) FROM mytable; # selects from db2.mytable
Using a USE statement to mark a specific current database does not prevent you from accessing tables in other databases. The following example can access the author table from the db1 database and the edit table from the db2 database:
mysql> USE db1; mysql> SELECT author_name,editor_name FROM author,db2.editor -> WHERE author.editor_id = db2.editor.editor_id;
To exit the database or connect to other databases, just user 'other database name'.
3.5 Current database selection
Command: mysql> select database();
The SELECT command in MySQL is similar to print or write in other programming languages. You can use it to display the results of a string, number, mathematical expression, etc. wait. How to use the special features of the SELECT command in MySQL?
(1). Display the MYSQL version
mysql> select version(); +-----------------------+ | version() | +-----------------------+ | 6.0.4-alpha-community | +-----------------------+ 1 row in set (0.02 sec)
(2). Display the current time
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2009-09-15 22:35:32 | +---------------------+ 1 row in set (0.04 sec)
(3). Display the year, month and day
SELECT DAYOFMONTH(CURRENT_DATE); +--------------------------+ | DAYOFMONTH(CURRENT_DATE) | +--------------------------+ | 15 | +--------------------------+ 1 row in set (0.01 sec) SELECT MONTH(CURRENT_DATE); +---------------------+ | MONTH(CURRENT_DATE) | +---------------------+ | 9 | +---------------------+ 1 row in set (0.00 sec) SELECT YEAR(CURRENT_DATE); +--------------------+ | YEAR(CURRENT_DATE) | +--------------------+ | 2009 | +--------------------+ 1 row in set (0.00 sec)
(4). Display the string
mysql> SELECT "welecome to my blog!"; +----------------------+ | welecome to my blog! | +----------------------+ | welecome to my blog! | +----------------------+ 1 row in set (0.00 sec)
(5). When calculating The tool uses
select ((4 * 4) / 10 ) + 25; +----------------------+ | ((4 * 4) / 10 ) + 25 | +----------------------+ | 26.60 | +----------------------+ 1 row in set (0.00 sec)
(6) to concatenate strings
select CONCAT(f_name, " ", l_name) AS Name from employee_data where title = 'Marketing Executive'; +---------------+ | Name | +---------------+ | Monica Sehgal | | Hal Simlai | | Joseph Irvine | +---------------+ 3 rows in set (0.00 sec)
Note: The CONCAT() function is used here to concatenate strings. In addition, we also used the AS we learned before to give the result column 'CONCAT(f_name, " ", l_name)' a pseudonym
4. Table operations
4.1 Create table
Command: create table