[Database Basics] Mysql download, installation and configuration

download

Download address: https://downloads.mysql.com/archives/community/

Insert image description here

The latest version is version 8.0. You can select the specified version in Product Version and select the installation platform in Operating System, as follows

Insert image description here

Install

MySQL installation files are divided into two types: .msi and .zip [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-4a5JfPM3-1691982442963) (MySql Download and Installation.assets/image -20200819100811960.png)]

  • .msi needs to be installed

  • The .zip format is decompressed by itself. After decompression, MySQL can actually be used, but environment variables must be configured as follows:

    ​ Open My Computer -> Right-click and select Properties -> Advanced -> Environment Variables -> System Variables -> Select Path and add: the path to the bin folder of mysql after it. My bin path is: D:\Program Files\mysql-8.0.20-winx64\bin

Insert image description here

Note: Try not to contain Chinese characters in the path

  • After configuring the environment variables, add a new configuration file mysql.ini in the D:\Program Files\mysql-8.0.20-winx64 directory, and create a data folder in the same directory as bin (used to store database data )

Insert image description here

  • The contents of the mysql.ini file are as follows

    [mysql]
    
    # 设置mysql客户端默认字符集
    default-character-set=utf8 
    
    [mysqld]
    
    #设置3306端口
    port = 3306 
    
    # 设置mysql的安装目录
    basedir=D:\Program Files\mysql-8.0.20-winx64
    
    # 设置mysql数据库的数据的存放目录
    datadir=D:\Program Files\mysql-8.0.20-winx64\data
    
    # 允许最大连接数
    max_connections=200
    
    # 服务端使用的字符集默认为8比特编码的latin1字符集
    character-set-server=utf8
    
    # 创建新表时将使用的默认存储引擎
    default-storage-engine=INNODB
    
  • Open the command prompt (CMD) as an administrator. There is no need to enter the installation directory (environment variables have been configured before). Enter the following command and press Enter. No response.

    mysqld --initialize-insecure --user=mysql
    

    Then enter the following command and press Enter. It will prompt that the installation is successful.

    mysqld install
    

Insert image description here

Summary of error messages:

  • A pop-up message appears during the installation process: VCRUNTIME140_1.dll cannot be found and the code cannot be continued. Reinstalling the program may resolve this issue as follows
    Insert image description here

    Solution: Install a collection of common Microsoft runtime libraries to solve the problem

  • Enter the command mysqld --install and an error message appears: Install/Remove of the Service Denied

    Solution: When opening the cmd.exe program, select "Open as administrator"

start up

  1. To start the service, enter the following command and press Enter. After successful startup, as shown below

    net start mysql
    

Insert image description here

  1. After the service is started successfully, enter the command when you need to log in (there is no password for the first login, just press Enter)

    Insert image description here

    Three ways to exit the current mysql:

    mysql退出三种方法:
        mysql > exit;
        mysql > quit;
        mysql > \q;
    

    Note: If unsuccessful, please execute Ctrl + C command first

    ​ Directly closing the cmd window will not exit. You have to enter exit to exit mysql.

  2. Stop service command

    net stop mysql
    

Insert image description here

Other configurations

  1. change Password

    To change the password (mysql must be started first), execute the following command and press Enter, enter password and press Enter. The password is generally set to 123456 for easy memory. Of course, you can also set the password according to your own habits.

    mysqladmin -u root -p password 12345
    

    If an error occurs when changing the mysql password:

    ​ mysqladmin: connect to server at ‘localhost’ failed;

    ​ ERROR:Access denied for user ‘root’@‘localhost’ (using password:YES)

    [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-px9Qh4W7-1691982442969) (MySql download and installation.assets/image-20200819111915811.png)]

    That is to say, the password modification failed. Please try the following methods to solve the problem:

    • First execute the command net stop mysql to stop the current service.

      [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-Pwk406jU-1691982442970) (MySql download and installation.assets/image-20200819112015367.png)]

    • Execute the command mysqld --user=mysql --skip-grant-tables --skip-networking& and restart the service

      [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-xmzWcj5n-1691982442971) (MySql download and installation.assets/image-20200819112116256.png)]

    • When logging in as root user, be sure to add mysql after the user name.

      [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-y9KsWN3j-1691982442972) (MySql download and installation.assets/image-20200819112222648.png)]

    • Then execute the update statement to modify the root user password.

      [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-SIt9QfkK-1691982442973) (MySql download and installation.assets/image-20200819112401396.png)]

      • If the above statement appears, it proves that the command execution is successful, otherwise it fails, as follows:

        An error occurred when changing the mysql password : ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corres

        image-20200819112324965**Solution:**Enter the following command under mysql

        image-20200819112855583
      • Execute quit or exit to log out of mysql

      • Stop the service again and restart

    • Try executing the command again to change the password

      [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-7r58fcGa-1691982442973) (MySql download and installation.assets/image-20200819113433089.png)]

      The above prompt appears, which proves that it can be modified normally and successfully!

  2. Turn off mysql auto-start at boot
    • Command line mode: Win + R to open Run, enter Services.msc in Run, and click Enter

      image-20200819105437668
    • Right-click My Computer on the desktop and select Manage

Insert image description here

  • After opening the service in both ways, pull down to find MySQL, double-click the startup type in the "General Panel" and select manual startup. At the same time, you can stop the currently running services.

MySql related commands

  • Configuration related commands:

    • Configure the bin directory to environment variables and install mysql

      mysqld --initialize-insecure --user=mysql   //回车没有反应,继续输入以下命令
      mysqld install  //安装mysql
      
    • net start mysql //Start the service

    • mysqladmin -u root -p password 12345 //Change the password of username root to 12345 (the original password is empty by default)

      mysqladmin -u username -p old password password new password // change password

    • mysql -u root -p // User login, the next step will prompt the user to enter the password

    • exit | quit | \q //Exit mysql command status

    • net stop mysql //Close service

  • Database operation commands

    • create database test; // Create a database and name it test
    • show databases; // List all databases
    • use database test; // Use the database named test
    • drop database test; //Delete the database named test
  • Data table operation related commands

    • create table demo(id int(10) not null auto_increment primary key, name varchar(20), pwd varchar(20) ) charset=gb2312; //Create a new table named demo. The new table contains three fields id, name, varchar and specify the field length, the primary key automatically grows character encoding gb2312
    • show tables; //List all tables under the current database
    • show columns from demo; //Display the data structure of the table named demo
    • insert into demo (name, psd) values ​​("Jack","12345"); //Insert data into the data table
    • select * from demo; //Read data table demo
    • delete from demo; // Clear data table demo
    • drop table demo; //Delete the table named demo
  • Back up and import database tables

    • Export database:

      1. Open the command line with cmd under windows

      2. cd to the bin directory of the MySQL installation directory. If the bin directory is added to the environment variable, there is no need to switch to the bin directory.

      3. Export database: mysqldump -u username -p database name > exported file name

        For example, enter the command mysqldump -u root -p person >d:\person.sql on the command line (you will be asked to enter the password to enter MySQL after entering it) (if you export a single table, enter the table name after the database name) )

      4. After the execution is completed, you will see the person.sql file in the d drive.

    • MySQL command line import database

      1. Enter MySQL: mysql -u username -p

        For example, the command line I entered: mysql -u root -p (after entering the same command, you will be asked to enter the MySQL password)

      2. Create a new database, which is an empty database at this time. For example, create a new target database named person.

      3. Input: mysql>use target database name

        For example, the command line I entered: mysql>use person;

      4. Import file: mysql>source imported file name;

        For example, the command line I entered: mysql>source d:/person.sql

Attachment: Mysql method and command to change user password

Method 1:
First log in to MySQL using the SET PASSWORD command.
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
take 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 authority 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.

Guess you like

Origin blog.csdn.net/qq_39335404/article/details/132272074