MySQL Tutorial 1.2

Compilation of netizens’ notes from the previous tutorial

MySQL reset password

If you forget your MySQL password, you can reset it by modifying the my.cnf file and adding skip-grant-tables. The steps are as follows:

1. Open the my.cnf configuration file, find [mysqld], and then add the following parameters below this line:

skip-grant-tables

Restart the MySQL service:

service mysql restart

Log in to MySQL, no password is required at this time, just press Enter:

# mysql -u root -p

Renewal root Secret 为 123456

mysql> use mysql;
mysql>  update user set authentication_string=password("123456") where user='root';
mysql> flush privileges;  # 刷新权限

Note that the password field name in version 5.7 is authentication_string, and the previous one was password .

After modification, remember to comment out the skip-grant-tables parameter in my.cnf , restart the MySQL service, and you can use Log in with the password you set.

Shut down the MySQL server:

service mysql stop

Enter the directory and start MySQL in safe mode

cd /usr/local/mysql/bin  
./mysqld_safe --skip-grant-tables & 

Notice:

Maybe your system does not have the safe_mysqld program (Windows or Ubuntu operating system), you can use mysqld:

mysqld --skip-grant-tables &

After startup, the root user logs in to mysql with a blank password:

# mysql -u root

mysql> update mysql.user set password=PASSWORD('123456') where User='root';   # 修改密码
mysql> flush privileges;  # 刷新权限
mysql> quit 

Start MySQL:

service mysql start

Now you can use the new password 123456 to log in.

If you know the password, you can use the following command:

# mysqladmin -u root -p password new password

If the method above fails to change the password (I did not), remember to change plugin to plugin when updating a>:mysql_native_password

update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';

 

After installation first, you will be prompted when executing any command:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

You can use the following command to change your password to 123456.

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

Then use the following command to refresh the permissions:

flush privileges;

Note the semicolon at the end of the directive.

Log out and log in again.

There is no data directory and my-default.ini file in the Windows 64-bit mysql 8.0.12 version package decompression, as well as solutions to the problem that the service cannot be started and methods to change the initial password.

1. There is no my-default.ini file. You can create a my.ini file in the root directory. The specific contents are as follows:

[mysql]
#Set the default character set of mysql client
default-character-set=utf8
 
[mysqld]
# Set port 3306
port = 3306
# Set the installation directory of mysql
basedir=C:\web\mysql-8.0.11
#Maximum number of connections allowed
max_connections=200
#The character set used by the server defaults to the 8-bit encoded latin1 character set
character-set-server=utf8
#The default storage engine that will be used when creating a new table
default-storage-engine=INNODB

2. If you set the storage directory for the data of the mysql database:

datadir=C:\web\sqldata

This will cause the service to fail to start. Do not add this sentence, and do not create a new data folder yourself. Just let mysql automatically generate the data.

Open the cmd command window as an administrator (open cmd directly to run, an error may be reported), and enter the bin directory of the mysql installation directory. Then enter the following command:

mysqld install
mysqld --initialize
net start mysql 

Finally, the data directory will be generated.

I have a problem installing mysql on Windows 10.

1. Coding error

mysql Found option without preceding group in config file D:\System\mysql\my.ini at line 1

Solution: Please convert the my.ini file format to ANSI encoding.

2. Time zone error

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

Need to add in the configuration file [mysqld] :

default-time-zone = '+8:00'

 

When typing any command, this error will be reported:

You must reset your password using ALTER USER statement before executing this statement.

This means that the password needs to be reset. The command to reset the password is as follows:

alter user user() identified by "密码";

For example:

alter user user() identified by "123456";

4 ways to change the root password in MySQL (taking Windows as an example)

Method 1: Use the SET PASSWORD command

First log in to MySQL.

Format:

mysql> set password for username@localhost = password('New password');

example:

mysql> set password for root@localhost = password('123');

Method 2: Use mysqladmin

Format:

mysqladmin -u username -p old password password new password

example:

mysqladmin -uroot -p123456 password 123

Method 3: Use UPDATE to directly edit the user table

First log in to MySQL.

mysql> use mysql; 
mysql> update user set password=password('123') where user='root' and host='localhost'; 
mysql> flush privileges; 

Method 4: When you forget the root password, you can do this, taking windows as an example:

  •  1. Close the running MySQL service.
  •  2. Open a DOS window and go to the mysql\bin directory.
  •  3. Enter mysqld --skip-grant-tables and press Enter. --skip-grant-tables means to skip the permission table authentication when starting the MySQL service.
  •  4. Open another DOS window (because the DOS window just now cannot be moved) and go to the mysql\bin directory.
  •  5. Enter mysql and press Enter. If successful, the MySQL prompt > will appear.
  •  6. Connect to the permission database: use mysql;.
  •  6. Change password: update user set password=password("123") where user="root" (don’t forget to add the semicolon at the end).
  •  7. Refresh privileges (required step): flush privileges;.
  •  8. Quit.
  •  9. Log out of the system, re-enter, and log in using the username root and the new password 123 you just set.

 

2059 error occurs when Navicat connects to Mysql8.0.11

When using MySQL 8+ or above, when navicat premium connects to the mysql database, a 2059 error will be reported;

Reason: The password encryption rule used by version 8+ is caching_sha2_password.

The navicate driver currently does not support the new encryption rules. There are two ways to solve the problem. One is to upgrade the navicat driver, and the other is to restore the mysql user login password encryption rule to mysql_native_password. Here we modify the mysql rule back to the previous mysql_native_password.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password' PASSWORD EXPIRE NEVER; # Modify encryption rules< a i=1>
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password'; # Update the user's password 
FLUSH PRIVILEGES; #Refresh permissions

To change the password for versions 8.0 and above, use:

ALTER user 'root'@'localhost' IDENTIFIED BY 'your password';

like:

mysql> ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;

Using the previous method will report this error:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

Note that there must be a semicolon.

In some versions above 8.0, using the mysqld --skip-grant-tables command to skip grant table authentication may fail without providing any error.

At this time you can use:

mysqld --console --skip-grant-tables --shared-memory

 

MySQL Management


Start and shut down the MySQL server

Under Windows system

Under Windows system, open the command window (cmd) and enter the bin directory of the MySQL installation directory.

start up:

cd c:/mysql/bin
mysqld --console

closure:

cd c:/mysql/bin
mysqladmin -uroot shutdown

Under Linux system

First, we need to check whether the MySQL server is started by running the following command:

ps -ef | grep mysqld

If MySql has been started, the above command will output the mysql process list. If mysql has not been started, you can use the following command to start the mysql server:

root@host# cd /usr/bin
./mysqld_safe &

If you want to shut down the currently running MySQL server, you can execute the following command:

root@host# cd /usr/bin
./mysqladmin -u root -p shutdown
Enter password: ******

MySQL user settings

If you need to add a MySQL user, you only need to add a new user to the user table in the mysql database.

The following is an example of adding a user. The user name is guest, the password is guest123, and the user is authorized to perform SELECT, INSERT and UPDATE operations:

root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed

mysql> INSERT INTO user 
          (host, user, password, 
           select_priv, insert_priv, update_priv) 
           VALUES ('localhost', 'guest', 
           PASSWORD('guest123'), 'Y', 'Y', 'Y');
Query OK, 1 row affected (0.20 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT host, user, password FROM user WHERE user = 'guest';
+-----------+---------+------------------+
| host      | user    | password         |
+-----------+---------+------------------+
| localhost | guest | 6f8c114b58f2ce9e |
+-----------+---------+------------------+
1 row in set (0.00 sec)

When adding a user, be sure to use the PASSWORD() function provided by MySQL to encrypt the password. You can see in the above example that the user password after encryption is: 6f8c114b58f2ce9e.

Note:In MySQL5.7, the password of the user table has been replaced by authentication_string.

Note:The password() encryption function has been removed in 8.0.11 and can be replaced by the MD5() function.

Note:Note that you need to execute the FLUSH PRIVILEGES statement. After executing this command, the authorization table will be reloaded.

If you do not use this command, you will not be able to use the newly created user to connect to the mysql server unless you restart the mysql server.

You can specify permissions for the user when creating a user. In the corresponding permission column, set it to 'Y' in the insert statement. The list of user permissions is as follows:

  • Select_priv
  • Insert_priv
  • Update_priv
  • Delete_priv
  • Create_priv
  • Drop_priv
  • Reload_priv
  • Shutdown_priv
  • Process_priv
  • File_priv
  • Grant_priv
  • References_priv
  • Index_priv
  • Alter_priv

Another way to add a user is through the SQL GRANT command. The following command will add the user zara to the specified database TUTORIALS with the password zara123.

root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    -> ON TUTORIALS.*
    -> TO 'zara'@'localhost'
    -> IDENTIFIED BY 'zara123';

The above command will create a user information record in the user table in the mysql database.

Note: MySQL’s SQL statement ends with a semicolon (;).


/etc/my.cnf file configuration

Generally, you do not need to modify this configuration file. The default configuration of this file is as follows:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

In the configuration file, you can specify the directories where different error log files are stored. Generally, you do not need to change these configurations.


Commands to manage MySQL

The following lists commonly used commands when using Mysql database:

  • USE Database name :
    Select the Mysql database to be operated, and all Mysql commands after using this command All only for this database.

    mysql> use RUNOOB;
    Database changed
  • SHOW DATABASES:
    Lists the database list of the MySQL database management system.

    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | RUNOOB             |
    | cdcol              |
    | mysql              |
    | onethink           |
    | performance_schema |
    | phpmyadmin         |
    | test               |
    | wecenter           |
    | wordpress          |
    +--------------------+
    10 rows in set (0.02 sec)
  • SHOW TABLES:
    Displays all tables of the specified database. Before using this command, you need to use the use command to select the database to be operated.

    mysql> use RUNOOB;
    Database changed
    mysql> SHOW TABLES;
    +------------------+
    | Tables_in_runoob |
    +------------------+
    | employee_tbl     |
    | runoob_tbl       |
    | tcount_tbl       |
    +------------------+
    3 rows in set (0.00 sec)
  • SHOW COLUMNS FROM Data table:
    Display the attributes, attribute types, and primary key information of the data table, Whether it is NULL, default value and other information.

    mysql> SHOW COLUMNS FROM runoob_tbl;
    +-----------------+--------------+------+-----+---------+-------+
    | Field           | Type         | Null | Key | Default | Extra |
    +-----------------+--------------+------+-----+---------+-------+
    | runoob_id       | int(11)      | NO   | PRI | NULL    |       |
    | runoob_title    | varchar(255) | YES  |     | NULL    |       |
    | runoob_author   | varchar(255) | YES  |     | NULL    |       |
    | submission_date | date         | YES  |     | NULL    |       |
    +-----------------+--------------+------+-----+---------+-------+
    4 rows in set (0.01 sec)
  • SHOW INDEX FROM Data table:
    Displays detailed index information of the data table, including PRIMARY KEY ( primary key).

    mysql> SHOW INDEX FROM runoob_tbl;
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | runoob_tbl |          0 | PRIMARY  |            1 | runoob_id   | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    1 row in set (0.00 sec)
  • SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern'] \G:
    This command will output the performance and statistical information of the Mysql database management system.

    mysql> SHOW TABLE STATUS FROM RUNOOB; # Display information about all tables in database RUNOOB
    
    mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob%'; # Information about the table whose name starts with runoob
    mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob%'\G; # Add \G to print the query results in columns

Gif diagram demonstration:

Guess you like

Origin blog.csdn.net/SHADOW_xhx/article/details/134489503