Home Backend Development PHP Tutorial Mysql database MYSQL database beginners guide

Mysql database MYSQL database beginners guide

Jul 29, 2016 am 08:34 AM
mysql database

There are many friends who have installed mysql but don't know how to use it. In this article, we will learn some common MYSQL commands from connecting to MYSQL, changing passwords, adding users, etc.
1. 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 you can enter directly by pressing Enter. Now in MYSQL, the MYSQL prompt is: //from www.w3sky.com
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, the same applies to others)
3. Exit the MYSQL command: //from www.w3sky.com
exit (Enter)
2. 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
3. Add new users. (Note: Unlike the above, the following are commands in the MYSQL environment, so they 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 test1 with the password abc, so that he can log in on any host and have query, insert, modify, and delete permissions on all databases. First connect to MYSQL as the root user, and then type the following commands:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
But the added users in Example 1 are very dangerous , if you want someone to know 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 information. See Example 2 for the solution.
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 host where the MYSQL database 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 "";
In the previous article we talked about login, adding users, password changes and other issues. In the next article, we will 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.
1. Operation skills
1. If you forget to add a semicolon after pressing Enter when typing a command, you don’t need to type the command again, just type a semicolon and press Enter. In other words, 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 call up previous commands. But an old version of MYSQL that I used before does not support it. What I am using now is:
mysql-3.23.27-beta-win.
2. 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 //from www.w3sky.com
show tables;
3. Display the structure of the data table:
describe table name;
4. Create database:
create database database name;
5. Create table:
use database 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;
3. One database and table creation And instances of inserting data
drop database if exists school; //If SCHOOL exists, delete it from www.w3sky.com
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
//Following To insert fields
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: Table under construction (1) Set the ID to a numeric field with a length of 3: int (3) and let each record automatically add one: auto_increment cannot be empty: not null and make it the main field primary key (2) Set the NAME Set ADDRESS to a character field of length 50 for a character field of length 10 (3), 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.
You can also type the above command at the mysql prompt, 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 mysqlbin in DOS state, and then type the following command:
mysql -uroot -p password < c:school .sql
If successful, a blank row will be left without any display; 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).
4. Transfer text data to the database
1. The format that text data should conform to: 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 mysqlbin directory, and use it first Use command to print the library where the table is located.
5. Back up the database: (The command is executed in the mysqlbin directory of DOS)
mysqldump --opt school>school.bbb
Note: Back up the database school to the school.bbb file, school.bbb is a text file, the file name is arbitrary Take it, open it and see what new discoveries you will make.
Postscript: In fact, the operation of MYSQL database is similar to that of other SQL databases. You'd better find a book about SQL. I only introduce some basic ones here. In fact, that’s all I know, haha. The best MYSQL tutorial is the "MYSQL Chinese Reference Manual" translated by "Yan Zi". It is not only free and available for download on every relevant website, but it is also the most authoritative. Unfortunately, it is not in chm format like "PHP4 Chinese Manual", which is not convenient when searching for function commands

The above introduces the Mysql database MYSQL database beginner's guide, including the Mysql database content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP development practice: Use PHPMailer to send emails to users in the MySQL database PHP development practice: Use PHPMailer to send emails to users in the MySQL database Aug 05, 2023 pm 06:21 PM

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

Go language and MySQL database: How to separate hot and cold data? Go language and MySQL database: How to separate hot and cold data? Jun 18, 2023 am 08:26 AM

As the amount of data continues to increase, database performance has become an increasingly important issue. Hot and cold data separation processing is an effective solution that can separate hot data and cold data, thereby improving system performance and efficiency. This article will introduce how to use Go language and MySQL database to separate hot and cold data. 1. What is hot and cold data separation processing? Hot and cold data separation processing is a way of classifying hot data and cold data. Hot data refers to data with high access frequency and high performance requirements. Cold data

How to use MySQL database for time series analysis? How to use MySQL database for time series analysis? Jul 12, 2023 am 08:39 AM

How to use MySQL database for time series analysis? Time series data refers to a collection of data arranged in time order, which has temporal continuity and correlation. Time series analysis is an important data analysis method that can be used to predict future trends, discover cyclical changes, detect outliers, etc. In this article, we will introduce how to use a MySQL database for time series analysis, along with code examples. Create a data table First, we need to create a data table to store time series data. Suppose we want to analyze the number

To what extent can I develop MySQL database skills to be successfully employed? To what extent can I develop MySQL database skills to be successfully employed? Sep 12, 2023 pm 06:42 PM

To what extent can I develop MySQL database skills to be successfully employed? With the rapid development of the information age, database management systems have become an indispensable and important component in all walks of life. As a commonly used relational database management system, MySQL has a wide range of application fields and employment opportunities. So, to what extent do MySQL database skills need to be developed to be successfully employed? First of all, mastering the basic principles and basic knowledge of MySQL is the most basic requirement. MySQL is an open source relational database management

How to perform incremental data backup of MySQL database using Go language How to perform incremental data backup of MySQL database using Go language Jun 17, 2023 pm 02:28 PM

As the amount of data increases, database backup becomes more and more important. For the MySQL database, we can use the Go language to achieve automated incremental backup. This article will briefly introduce how to use Go language to perform incremental backup of MySQL database data. 1. Install the Go language environment. First, we need to install the Go language environment locally. You can go to the official website to download the corresponding installation package and install it. 2. Install the corresponding library. The Go language provides many third-party libraries for accessing MySQL databases, among which the most commonly used ones are

How to make reliable MySQL database connection using Go language? How to make reliable MySQL database connection using Go language? Jun 17, 2023 pm 07:18 PM

With the large amount of data that needs to be stored and processed, MySQL has become one of the most commonly used relational databases in application development. The Go language is becoming more and more popular among developers due to its efficient concurrency processing and concise syntax. This article will lead readers to implement reliable MySQL database connection through Go language, allowing developers to query and store data more efficiently. 1. Several ways for Go language to connect to MySQL database. There are usually three ways to connect to MySQL database in Go language, which are: 1. Third-party library

How to use MySQL database for image processing? How to use MySQL database for image processing? Jul 14, 2023 pm 12:21 PM

How to use MySQL database for image processing? MySQL is a powerful relational database management system. In addition to storing and managing data, it can also be used for image processing. This article will introduce how to use a MySQL database for image processing and provide some code examples. Before you begin, make sure you have installed a MySQL database and are familiar with basic SQL statements. Create a database table First, create a new database table to store the image data. The structure of the table can be as follows

MySQL database and Go language: How to perform data caching? MySQL database and Go language: How to perform data caching? Jun 17, 2023 am 10:05 AM

In recent years, the Go language has become increasingly popular among developers and has become one of the preferred languages ​​for developing high-performance web applications. MySQL is also a popular database that is widely used. In the process of combining these two technologies, caching is a very important part. The following will introduce how to use Go language to handle the cache of MySQL database. The concept of caching In web applications, caching is a middle layer created to speed up data access. It is mainly used to store frequently requested data to

See all articles