Summary of some common operations of MySQL under windows

table of Contents

One, mysql service start, stop, restart

2. Initialize the root password

Three, allow root remote access

Fourth, operate myseql to insert data in cmd

4.1, create a database

4.2, create a table

4.3, insert data

4.4, display table

Five, the isolation settings of mysql things

5.1, set the transaction isolation level

5.2, reset isolation level

5.3, commit the transaction

Six, the stored procedure of mysql things

Seven, mysql view

Eight, mysql backup and restore

8.1, use the mysqldump command to backup

8.2, directly copy the entire database directory

8.3, use the mysqlhotcopy tool to quickly backup

8.4, restore


 

One, mysql service start, stop, restart

Start: net start mysql
stop: net stop mysql
restart: Windows cannot directly restart (restart), you can only stop and then start.

2. Initialize the root password

Enter the mysql database: mysql -u root -p

Get the initial password: mysqld --initialize --user=mysql --console

Execute the following command to modify the default password, where '123456' can be changed to your own password

mysql>alter user user() identified by "123456";

Three, allow root remote access

For example, you want root to use 123456 to connect to the mysql server from any host.
mysql> GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY'123456' WITH GRANT OPTION;
mysql> flush privileges;

If you want to allow the user linuxidc to connect to the mysql server from the host whose ip is 192.168.119.10 and use 654321 as the password
mysql> GRANT ALL PRIVILEGES ON *.* TO'linuxidc'@'192.168.119.10' IDENTIFIED BY'654321' WITH GRANT OPTION;
mysql> flush privileges;

Fourth, operate myseql to insert data in cmd

4.1, create a database

mysql> create database accout;

4.2, create a table

mysql> create table accout_tb1
    -> (
    -> id INT NOT NULL,
    -> name CHAR(100) NULL,
    -> balance INT NOT NULL
    -> );

4.3, insert data

mysql> insert into accout_ta1 (id,name,balance) values (1,'lilei',450);

4.4, display table

mysql> select * from accout_tb1;

Five, the isolation settings of mysql things

5.1, set the transaction isolation level

*读未提交(read-uncommitted)
mysql>  set session transaction isolation level read uncommitted;
*不可重复读(read-committed)
mysql>  set session transaction isolation level read committed;
*可重复读(repeatable-read)
mysql>  set session transaction isolation level repeatable read;
*串行化(serializable)
mysql>  set session transaction isolation level serializable;

5.2, reset isolation level

mysql> start transaction;

5.3, commit the transaction

mysql> commit;

Six, the stored procedure of mysql things

  • Declaration statement terminator, used to distinguish

DELIMITER //

  • Declare a stored procedure

CREATE PROCEDURE demo_in_parameter(IN p_in int)

  • Stored procedure start and end symbols

BEGIN .... END

  • Variable assignment

SET @p_in=1

Example:
*IN input parameter:
Indicates that the value of the parameter must be specified when calling the stored procedure. Modifying the value of the parameter in the stored procedure cannot be returned. It is the default value.
Create:
DELIMITER //  
CREATE PROCEDURE demo_in_parameter(IN p_in int)  
    BEGIN   
    SELECT p_in;   
    SET p_in=2;   
    SELECT p_in;   
    END//  
DELIMITER; 

Execute:
mysql> SET @p_in=1;  
mysql> CALL demo_in_parameter(@p_in);  
*OUT output parameter:
the value can be changed inside the stored procedure, And return to
create:
DELIMITER //  
CREATE PROCEDURE demo_out_parameter(OUT p_out int)  
    BEGIN 
    SELECT p_out;  
    SET p_out =
    SELECT p_out;  
    END //  
DELIMITER; 

Execute:
mysql> SET @p_inout=1;  
mysql> CALL demo_inout_parameter(@p_inout);
*INOUT input and output parameters:
specified when calling, and can be changed and returned
Create:
DELIMITER //   
CREATE PROCEDURE demo_inout_parameter(INOUT p_inout int)   
BEGIN 
    SELECT p_inout;  
    SET p_inout=2;  
    SELECT p_inout;   
 END //   
DELIMITER; 

Execute:
mysql> SET @p_inout=1; 
mysql> CALL demo_inout_parameter(@p_inout);


Seven, mysql view

(1) Create view
CREATE VIEW <view name> AS <SELECT statement>
<view name>: Specify the name of the view. The name must be unique in the database and cannot be the same name as other tables or views.
<SELECT statement>: Specify the SELECT statement to create the view, which can be used to query multiple base tables or source views

 The specification of the SELECT statement has the following restrictions:
In addition to the CREATE VIEW privilege, the user also has the related privileges of the underlying tables and other views involved in the operation.
The SELECT statement cannot reference system or user variables.
The SELECT statement cannot contain subqueries in the FROM clause.
The SELECT statement cannot reference prepared statement parameters.

The table or view referenced in the view definition must exist. However, after the view is created, you can delete the table or view referenced by the definition. You can use the CHECK TABLE statement to check the view definition for such problems.

例子:
mysql> create view view_accout_tb1
    -> as select * from accout_tb1;

mysql> select * from view_accout_tb1;

By default, the fields of the created view and the base table are the same. You can also create a view by specifying the name of the view field.

mysql> create view view_accout_tb2
    -> as select id,name from accout_tb1;

(2) Query the
view name of the DESCRIBE view;

Views are mainly used for querying in the following areas:
* Use the view to reformat the retrieved data.
* Use views to simplify complex table joins.
* Use the view to filter the data.

mysql> describe view_accout_tb2;

Eight, mysql backup and restore

8.1, use the mysqldump command to backup

The mysqldump command backs up the data in the database into a text file. The structure of the table and the data in the table will be stored in the generated text file.

The working principle of the mysqldump command is very simple. It first finds out the structure of the table to be backed up, and then generates a CREATE statement in the text file.
Then, convert all the records in the table into an INSERT statement. Then through these statements, you can create tables and insert data.


(1) Back up a database
mysqldump -u username -p dbname table1 table2 ... -> BackupName.sql

*dbname parameter indicates the name of the database;
*table1 and table2 parameters indicate the name of the table that needs to be backed up, if it is empty, the entire database is backed up;
*BackupName.sql parameter table design the name of the backup file, the file name can be preceded by an absolute path. The database is usually divided into a file with the suffix sql;

例子:
F:\myseql\mysql-8.0.19-winx64\bin>mysqldump -u root -p accout accout_tb1 > backup.sql

(2) Back up multiple databases
F:\myseql\mysql-8.0.19-winx64\bin> mysqldump -u username -p --databases dbname2 dbname2> Backup.sql

(3) Back up all databases
mysqldump -u username -p -all-databases> BackupName.sql

例子:
F:\myseql\mysql-8.0.19-winx64\bin>mysqldump -u -root -p -all-databases > D:\all.sql

8.2, directly copy the entire database directory


MySQL has a very simple backup method, which is to copy the database files in MySQL directly. This is the simplest and fastest method.

But before that, you must stop the server first, so that you can ensure that the data in the database will not change during the replication period. If
data is written in the process of copying the database , it will cause data inconsistency. This situation is fine in a development environment, but it is difficult to allow a backup server in a production environment.

Note: This method does not apply to InnoDB storage engine tables, but it is very convenient for MyISAM storage engine tables. At the same time, the MySQL version should preferably be the same during restoration.

8.3, use the mysqlhotcopy tool to quickly backup

Just look at the name and you know it is a hot backup. Therefore, mysqlhotcopy supports backup without stopping the MySQL server. Moreover, the backup method of mysqlhotcopy is faster than mysqldump. mysqlhotcopy is a perl script, mainly used under Linux systems. It uses LOCK TABLES, FLUSH TABLES and cp for fast backup.

Principle: First add a read lock to the database that needs to be backed up, then use FLUSH TABLES to write the data in the memory back to the database on the hard disk, and finally,
copy the database file that needs to be backed up to the target directory.

The command format is as follows:
mysqlhotcopy [option] dbname1 dbname2 backupDir/

*dbname: database name;
*backupDir: which folder to backup to;

Common options:
--help: view mysqlhotcopy help;
--allowold: if the same backup file exists in the backup directory, add _old to the old backup file;
--keepold: if the same backup file exists in the backup directory, no Delete the old backup file, but rename the old file;
--flushlog: After this generation, the update to the database will be recorded in the log;
--noindices: Only the data file is backed up, not the index file;
--user =Username: Used to specify the user name, you can use -u instead;
--password=Password: Used to specify the password, you can use -p instead. When using -p, there is no space between the password and -p;
--port=port number: used to specify the access port, you can use -P instead;
--socket=socket file: used to specify the socket file, you can use -S instead;

mysqlhotcopy does not come with mysql, you need to install the Perl database interface package; the download address is: http://dev.mysql.com/downloads/dbi.html At present, this tool can only backup MyISAM tables.

8.4, restore

(1) Restore the backup using the mysqldump command

The syntax of the database is as follows:
mysql -u root -p [dbname] <backup.sq

Example:
mysql -u root -p <C:\backup.sql

(2) Restore the backup of the direct copy directory
When restoring in this way, you must ensure that the version numbers of the two MySQL databases are the same. The MyISAM type table is valid, and the InnoDB type table is not available.
The table space of the InnoDB table cannot be copied directly.

Guess you like

Origin blog.csdn.net/wxplol/article/details/104918351