Home Database Mysql Tutorial Mysql Series (6) Basics for Beginners

Mysql Series (6) Basics for Beginners

Jan 22, 2017 pm 04:42 PM

Connect to MySQL

Format: mysql -h host address -u username -p user password

1. Example 1: Connect to MYSQL on this machine.

First open the DOS window, then enter the directory mysqlbin, and then type the command mysql -uroot -p. After pressing Enter, you will be prompted to enter your password. If MYSQL has just been installed, the super user root does not have a password, so directly Press Enter to enter MySQL. The MySQL prompt is: mysql>

2. Example 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 -uroot -pabcd123

(Note: u and root do not need to add spaces, and the same applies to others)

3 . Exit MySQL command: exit (Enter)

Note: If you want to successfully connect to the remote host, you need to open MySQL remote access permission on the remote host

The method is as follows:

In Enter the remote host as an administrator

Enter the following command

mysql>GRANT ALL PRIVILEGES ON *.* TO 'agui'@%'IDENTIFIEDBY '123' WITH GRANT OPTION;

FLUSH PRIVILEGES;

//Give any host permission to access data

mysql>FLUSH PRIVILEGES

//Modification takes effect

agui for us The username

password used is 123

that is: after setting the settings on the remote host, we can connect to the remote host through mysql -h110.110.110.110 -uagui -p123

Change password

Format: mysqladmin -u username -p old password password new password

1. Example 1: Add a password ab12 to root. First, enter the directory mysqlbin under DOS, and then type the following command

mysqladmin -uroot -password ab12

Note: Because root does not have a password at the beginning, the -p old password item can be omitted. .

2. Example 2: Change the root password to djg345.

mysqladmin -uroot -pab12 password djg345

Add new user

(Note: Different from the above, the following is a command in the MySQL environment , so there is a semicolon after it as the command terminator)

Format: grant select on database.* to username@login host identified by “password”

Example 1. Add a user The password of test1 is abc, which allows him to log in on any host and has query, insert, modify, and delete permissions on all databases. First connect to MySQL as the root user, and then type the following command:

grant select,insert,update,delete on *.* to test1@“%” Identified by “abc”;

But the user added in Example 1 is very dangerous. If someone knows the password of test1, then he can log in to your mysql database on any computer on the Internet and do whatever he wants with your data. Solution See example 2.

Example 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 MYSQL database The host where it is located), so that 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 test2@localhost 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 test2@localhost identified by “”;

Let’s take a look at the database operations in MySQL. Note: You must first log in to MySQL. The following operations are performed at the MySQL prompt, and each command ends with a semicolon.

Operation skills

1. If you find that you forgot to add a semicolon when typing a command and press Enter, you do not need to type the command again. You only need to add a semicolon and press Enter. That is to say, you can divide a complete command into several lines and use a semicolon as the end mark.

2. You can use the cursor up and down keys to recall previous commands. But an old version of MySQL I used before does not support it. I am using mysql-3.23.27-beta-win.

Display command

1. Display the database list.

show databases;

There were only two databases at the beginning: mysql and test. The mysql library is very important. It contains MySQL system information. When we change passwords and add new users, we actually use this library for operations.

2. Display the data tables in the library:

use mysql; //Open the library, it will be familiar to those who have learned FOXBASE

show tables;

3. Display the structure of the data table:

describe table name;

4. Create database:

create database library name;

5. Create table:

use library name;

create table table name (field setting list);

6. Delete database and table:

drop database database name;

drop table table name;

7. Clear the records in the table:

delete from table name;

8. Display records in the table:

select * from table name;

9. Display errors, warnings and notifications generated by the last executed statement:

show warnings;

10. Only the errors generated by the last executed statement are displayed:

show errors;

Instance

drop database if exists school; //If SCHOOL exists Then delete

create database school; //Create library SCHOOL

use school; //Open library SCHOOL

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

); //End of table creation

//The following are the inserted fields

insert into teacher values('','glchengang','XX company', '1976-10-10');

insert into teacher values('','jack','XX company','1975-12-23');

Note: in Creating table:

1. Set the ID to a numeric field of length 3: int(3) and let each record automatically increase by one: auto_increment cannot be empty: not null and make it the primary field primary key.

2. Set NAME to a character field of length 10.

3. Set ADDRESS to a character field of length 50, and the default value is Shenzhen. What is the difference between varchar and char? I will have to wait for a future article to talk about it.

4. Set YEAR as the date field.

If you type the above command at the mysql prompt, it is OK, but it is not convenient for debugging. You can write the above command as it is into a text file, assuming it is school.sql, then copy it to c:\, enter the directory \mysql\bin in DOS state, and then type the following command:

mysql - uroot -p password< c:\school.sql

If successful, a blank line will be left blank; if there is an error, there will be a prompt. (The above command has been debugged, you only need to remove the // comment to use it).

Text to database

1. The text data should conform to the format: field data are separated by tab keys, and null values ​​are replaced by \n.

Example:

3. rose Shenzhen No. 2 Middle School 1976-10-10

4. mike Shenzhen No. 1 Middle School 1975-12-23

2. Data input command load data local infile "file name" into table table name;

Note: You'd better copy the file to the \mysql\bin directory, and first use the use command to open the database where the table is located

Or transfer (cd) the path to the path where the text is to be imported before logging in to the database.

Backup database

(The command is executed in the \mysql\bin directory of DOS)

mysqldump --opt school>school.bbb

Note: Back up the school database to the school.bbb file. School.bbb is a text file. You can choose any file name. Open it and see if you will find new things.

Method to automatically back up mysql database

1. First create a batch file and save the following code as a .bat file. The file name is preferably in English. Pay attention to the path below. Taking the author's own database as an example, the database is installed under mysql\mysql under the D drive. The backup location is F:\beifen. The following code is the date [8].

@echo off[9]

color 0D

MODE con: COLS=71 LINES=25

title mysql database automatic backup script (task plan )--Script author: http://www. ***. com

set sou_dir="D:\mysql\Mysql\data"

set obj_dir=F:\beifen\%date:~0,10%

net stop mysql

md %obj_dir%

xcopy /e /y %sou_dir% %obj_dir%

net start mysql

@echo off&setlocal enabledelayeexpansion

call:D,30

echo. The date 30 days ago is: %D%

echo. Delete the backup 30 days ago...

if exist F:\beifen\%D% rd /s /q F:\beifen\%D%

echo The automatic backup is completed and the program will automatically exit...

Restore/Import

Restore/Import database [10] During the import process, enter the mysql database console, such as mysql -u root -p

mysql>use database

Then use the source command, and the following parameters are script files (such as .sql used here)

mysql>source d:\dbname.sql

If the prompt cannot be found file, after inputting the source, you can use the mouse to drag the file directly into the command line window

Start the tracking file

mysqld --debug

Shut down the server

mysqladmin -u root shutdown

Start the MySQL service

mysqld --console

The above is the basic content of Mysql series (6) for beginners. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

See all articles