MySQL environment construction - "MySQL database"

Hello everyone uu from CSDN, Xiao Yalan is here again, I haven’t updated the article for a long time, let’s continue today! ! ! Today Xiaoyalan’s content is about setting up the MySQL environment. Next, let’s enter the world of the MySQL database.


MySQL uninstall

MySQL download, installation, configuration

MySQL login

MySQL demo use

MySQL graphical management tool

MySQL directory structure and source code


MySQL uninstall

Step 1: Stop the MySQL service

Before uninstalling, stop the service of MySQL8.0. Press the "Ctrl + Alt + Delete" key combination on the keyboard to open the "Task Manager" dialog box, and you can find the "MySQL8.0" service in the "Services" list. Service, select the "Stop" option to stop the service of MySQL8.0, as shown in the figure.

 Step 2: Uninstall the software

Method 1: Through the control panel method

To uninstall the MySQL8.0 program, you can directly select "Uninstall a Program" in the "Control Panel" like other desktop applications, and find the MySQL8.0 server program in the program list, and double-click to uninstall it, as shown in the figure. In this way, the data in the data directory will not be deleted.

 Method 2: Uninstall through software such as 360 or computer manager

Method 3: Uninstall through the uninstall function provided by the installation package

You can also uninstall the MySQL8.0 server program through the installation wizard.

① Double-click the downloaded mysql-installer-community-8.0.26.0.msi file again to open the installation wizard. The installation wizard will automatically detect the installed MySQL server program.

② Select the MySQL server program to be uninstalled, and click "Remove" to uninstall.

 ③ Click the "Next" button to confirm the uninstallation.

 ④ A selection window pops up whether to remove the data directory at the same time. If you want to delete the data in the MySQL server at the same time, check "Remove the data directory", as shown in the figure.

⑤ Execute uninstallation. Click the Execute button to uninstall.

 

⑥ Complete the uninstallation. Click the "Finish" button and that's it. If you want to uninstall the installation wizard program of MySQL8.0 at the same time, check "Yes, Uninstall MySQL Installer", as shown in the figure.

Step 3: Cleanup of residual files 

If the reinstallation is unsuccessful, you can uninstall and clean up the remaining files before reinstalling.

(1) Service directory: the installation directory of the mysql service

(2) Data directory: C:\ProgramData\MySQL by default

If you have specified the data directory separately, you can find your own data directory and delete it.

Note: Please make a data backup before uninstalling

After the operation, you need to restart the computer, and then install it. If the installation still fails, continue to step 4 below.

Step 4: Clean up the registry (optional)

If you have done the previous steps and the installation still fails, you can clean up the registry.

How to open the Registry Editor: Type regedit   in the system's search box

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\MySQL service directory delete

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\Eventlog\Application\MySQL service directory delete

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\MySQL service directory delete

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\MySQL service directory delete

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL service delete

ControlSet001 and ControlSet002 in the registry are not necessarily 001 and 002, they may be ControlSet005, 006 and the like  

Step 5: Delete the environment variable configuration

Find the path environment variable, and delete the environment variable about mysql, remember not to delete them all.

For example: delete D:\develop_tools\mysql\MySQLServer8.0.26\bin; this part


MySQL download, installation, configuration

  • MySQL Community Server community version , open source and free, free to download, but does not provide official technical support, suitable for most ordinary users.
  • MySQL Enterprise Edition The enterprise version requires payment and cannot be downloaded online. You can try it for 30 days. It provides more functions and more complete technical support, and is more suitable for enterprise customers who have higher requirements for database functions and reliability.
  • MySQL Cluster cluster version , open source and free. It is used to set up a cluster server, and several MySQL Servers can be packaged into one Server. It needs to be used on the basis of Community Edition or Enterprise Edition.
  • MySQL Cluster CGE Premium Cluster Edition , for a fee.

In addition, the official also provides MySQL Workbench (GUITOOL), a graphical interface management tool specially designed for MySQL.

MySQL Workbench is divided into two versions, namely the community version (MySQL Workbench OSS) and the commercial version (MySQL WorkbenchSE).

software download

1. Download address

Official website: https://www.mysql.com

2. Open the official website and click DOWNLOADS

Then, click MySQL Community(GPL) Downloads

3. Click MySQL Community Server

 

 4. Select the appropriate version in General Availability (GA) Releases

Two installation files are provided under the Windows platform: MySQL binary distribution version (.msi installation file) and free installation version (.zip compressed file). Generally speaking, you should use the binary distribution, because this version provides a graphical installation wizard process, which is easier to use than other distributions, and you can run MySQL without starting other tools.

  • It is recommended to download the MSI installer under the Windows system; click Go to Download Page to download

 

  •  There are two installation programs for MySQL8.0 installation under Windows

        mysql-installer-web-community-8.0.26.0.msi Download program size: 2.4M; installation requires networking to install components.

        mysql-installer-community-8.0.26.0.msi Download program size: 450.7M; it can be installed offline during installation. recommend.

  • If you install MySQL5.7 version, select Archives, and then select the corresponding version of MySQL5.7. Download the latest version of MySQL5.7.34 here.

 

Installation of MySQL8.0 version

After the MySQL download is complete, find the downloaded file and double-click to install it. The specific operation steps are as follows.

Step 1: Double-click the downloaded mysql-installer-community-8.0.26.0.msi file to open the installation wizard.

Step 2: Open the "Choosing a Setup Type" (select the installation type) window, in which 5 types of installation are listed, namely Developer Default (default installation type), Server only (only as a server), Client only (only as a Client), Full (full installation), Custom (custom installation). Here select the "Custom (custom installation)" type button, click the "Next (next step)" button.

Step 3: Open the "Select Products" (select products) window, you can customize the list of products that need to be installed. For example, after selecting "MySQL Server 8.0.26-X64", click the "→" Add button to choose to install the MySQL server, as shown in the figure. Using a generic approach, you can add other products that you need to install.

 At this time, if you directly "Next" (next step), the installation path of the product is the default. If you want to customize the installation directory, you can select the corresponding product, and then a hyperlink of "Advanced Options" (advanced options) will appear below.

ProgramData directory (this is a hidden directory). If you customize the installation directory, please avoid the "Chinese" directory. In addition, it is recommended that the service directory and data directory be stored separately.

Step 4: After selecting the product to be installed in the previous step, click "Next" to enter the confirmation window, as shown in the figure. Click the Execute button to start the installation.

Step 5: After the installation is complete, "Complete" will be displayed under the "Status" list, as shown in the figure.

 

Configure MySQL8.0

After MySQL is installed, the server needs to be configured. The specific configuration steps are as follows.  

Step 1: In the last step of the previous section, click the "Next" button to enter the product configuration window.

 

Among them, the "Config Type" option is used to set the type of the server. Click the lower triangle button to the right of the option to view 3 options, as shown in the figure.

  • Server Machine (server) : This option represents the server, and the MySQL server can run together with other server applications, such as Web servers. The MySQL server is configured with appropriate proportions of system resources.
  • Dedicated Machine (dedicated server) : This option represents a server that only runs the MySQL service. The MySQL server is configured to use all available system resources.

Step 3: Click the "Next" button to open the window for setting the authorization method. Among them, the above option is a new authorization method provided by MySQL8.0, which adopts the SHA256-based password encryption method; the following option is the traditional authorization method (compatibility with version 5.x is reserved).

Step 4: Click the "Next" button to open the window for setting the password of the server root super administrator. As shown in the figure, you need to enter the same login password twice. You can also add other users through "Add User". When adding other users, you need to specify the user name, which host/hosts the user name is allowed to log in on, and user roles can also be specified. No users are added here.

Step 5: Click the "Next" button to open the Set Server Name window, as shown in the figure. The service name will appear in the Windows service list, and can also be used in the command line window to start and stop the service. This book sets the service name to "MySQL80". If you want to start the service automatically at startup, you can also check the "Start the MySQL Server at System Startup" option (recommended).

 The following is the choice of how to run the service? You can choose one of "Standard System Account" (standard system user) or "Custom User" (custom user). The former is recommended here.

 

Step 7: Complete the configuration as shown in the figure. Click the "Finish" button to complete the configuration of the server.

 

Step 8: If there are other products that need to be configured, you can select other products and continue to configure. If not, simply select "Next" (next step) and go straight through the entire installation and configuration process.

Step 9: Finish the installation and configuration.

If you do not configure the MySQL environment variable, you cannot directly enter the MySQL login command on the command line. Here's how to configure MySQL environment variables:

Step 1: Right-click the [This PC] icon on the desktop, and select the [Properties] menu command in the pop-up shortcut menu.

Step 2: Open the [System] window, click the link of [Advanced System Settings].

Step 3: Open the [System Properties] dialog box, select the [Advanced] tab, and click the [Environment Variables] button.

Step 4: Open the [Environment Variables] dialog box, and select the path variable in the system variable list.

Step 5: Click the [Edit] button, and in the [Edit Environment Variable] dialog box, add the bin directory of the MySQL application (C:\Program Files\MySQL\MySQL Server 8.0\bin) to the variable value, using the number to separate it from other paths.

Step 6: After the addition is complete, click the [OK] button to complete the operation of configuring the path variable, and then you can directly enter the MySQL command to log in to the database.

Installation and configuration of MySQL5.7 version

Install

The installation process of this version is the same as the above process except the version number is different. So the installation screenshot of MySQL5.7.34 version is omitted here.

configuration

The configuration link is slightly different from the MySQL8.0 version. In most cases, you can directly select "Next", which will not affect the use of finishing. When configuring MySQL5.7 here, it is important to emphasize that the same port number cannot be used with the previously installed MySQL8.0.

Installation failure problem

The installation and configuration of MySQL is a very simple matter, but problems may also arise during operation, especially for beginners.

Question 1: The MySQL8.0 software installation package cannot be opened or fails during the installation process. How to solve it?

Before running the MySQL8.0 software installation package, the user needs to ensure that the .Net Framework related software has been installed in the system. If this software is missing, the MySQL8.0 software cannot be installed normally.

In addition, make sure that Windows Installer is installed normally. Installing mysql8.0 on windows requires the operating system to have Microsoft Visual C++ 2015-2019 installed in advance.

 

Question 2: Failed to uninstall and reinstall MySQL? 

This problem is usually caused by the fact that related information is not completely cleared when MySQL is uninstalled.

The solution is to delete the previous installation directory. If the service installation directory has not been specified separately in the previous installation, the default installation directory is "C:\Program Files\MySQL", delete this directory completely. At the same time, delete the MySQL Data directory. If the data directory has not been specified in the previous installation, the default installation directory is "C:\ProgramData\MySQL", which is generally a hidden directory. After deleting, reinstall it.

Question 3: How to delete the list of MySQL services that have not been uninstalled cleanly in the Windows system?

The operation method is as follows, enter "cmd" in the "search box" of the system, press the "Enter" key to confirm, and the command prompt interface will pop up. Then enter "sc delete MySQL service name" and press the "Enter" key to completely delete the remaining MySQL service.


MySQL login

Service start and stop

After MySQL is installed, the server process needs to be started, otherwise the client cannot connect to the database.

In the previous configuration process, MySQL has been installed as a Windows service, and MySQL is automatically started and stopped when Windows starts and stops.

Method 1: Use GUI tools

  • Step 1: Open windows services
  • Way 1: Computer (click the right mouse button) → Manage (click) → Services and Applications (click) → Services (click)
  • Method 2: Control Panel (click) → System and Security (click) → Administrative Tools (click) → Services (click)
  • Method 3: Task bar (click the right mouse button) → start task manager (click) → service (click)
  • Method 4: Click the [Start] menu, enter "services.msc" in the search box, and press Enter to confirm
  • Step 2: Find MySQL80 (click the right mouse button) → start or stop (click)

Method 2: Use the command line 

# 启动 MySQL 服务命令:
net start MySQL服务名

# 停止 MySQL 服务命令:
net stop MySQL服务名

 

  • The service name after start and stop should be consistent with the service name specified in the previous configuration.
  • If you are prompted with "denial of service" after entering the command, please open the command prompt interface as a system administrator and try again.

 Login and logout of the built-in client

After the MySQL service is started, you can log in to the MySQL database through the client. Note: Make sure the service is turned on.

Login method 1: MySQL built-in client

Start Menu → All Programs → MySQL → MySQL 8.0 Command Line Client

Note: For root users only

  • Format:

        mysql -h host name -P port number -u username -p password

  • Example:

        mysql -h localhost -P 3306 -u root -pabc123 # The password of the root user I set here is abc123

 Notice:

(1) There can be no space between -p and the password, and there can be or no space between other parameter names and parameter values.

mysql -hlocalhost -P3306 -uroot -pabc123  

(2) It is recommended to enter the password on the next line to ensure security

mysql -h localhost -P 3306 -u root -p

Enter password:****  

(3) The client and server are on the same machine, so enter localhost or the IP address 127.0.0.1. At the same time, because it is connecting to the local machine: -hlocalhost can be omitted, if the port number is not modified: -P3306 can also be omitted  

Abbreviated as:

mysql -u root -p

Enter password:****

 After the connection is successful, there is information about the MySQL Server service version, as well as the id of the first connection.

You can also obtain the MySQL Server service version information on the command line in the following ways:

c:\> mysql -V

c:\> mysql --version  

Or after logging in , check the current version information by:

 mysql> select version();

exit

or

quit


MySQL demo use

Demo of using MySQL

1. View all databases

show databases;

"information_schema" is the built-in database of the MySQL system, which mainly stores the system information of the MySQL database server, such as the name of the database, the name of the data table, the name of the field, access rights, the folder where the data file is located, and the folder used by the system. etc.

"performance_schema" is the built-in database of the MySQL system, which can be used to monitor various performance indicators of MySQL.

The "sys" database is the built-in database of the MySQL system. Its main function is to display various performance indicators of the MySQL database server in an easier-to-understand manner, and to help system administrators and developers monitor the technical performance of MySQL.

The "mysql" database stores system information needed by the MySQL database server to run, such as the data folder, the character set currently in use, constraint checking information, etc.

Why do we only see the two databases "demo" and "sys" in Workbench?

This is because Workbench is a graphical management tool, mainly for developers, and the two databases "demo" and "sys" are enough. If there are special requirements, such as monitoring various performance indicators of the MySQL database, directly operating MySQL database system files, etc., the DBA can view other system databases through SQL statements.  

2. Create your own database

create database 数据库名;

#创建atguigudb数据库,该名称不能与已经存在的数据库重名。
create database atguigudb;

3. Use your own database

use 数据库名;

#使用atguigudb数据库
use atguigudb;

Note: If the use statement is not used, and the subsequent operations on the database are not limited by the "data name", then "ERROR 1046 (3D000): No database selected" will be reported (no database selected)

After using the use statement, if the next SQL is for one database operation, then there is no need to repeat the use, if it is for another database operation, then use it again.

4. View all tables of a library

show tables from 数据库名;

5. Create a new table

create table 表名称(
 字段名 数据类型,
 字段名 数据类型
);

Note: If it is the last field, add a comma after it, because the function of the comma is to separate each field.

#创建学生表
create table student
(
 id int,
   name varchar(20)  #说名字最长不超过20个字符
);

 6. View the data of a table

select * from 数据库表名称;
#查看学生表的数据
select * from student;

7. Add a record

insert into 表名称 values(值列表);

#添加两条记录到student表中
insert into student values(1,'张三');
insert into student values(2,'李四');

Error:

mysql> insert into student values(1,'张三');
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at 
row 1

mysql> insert into student values(2,'李四');
ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'name' at 
row 1

mysql> show create table student;

Character set problem.

8. View the creation information of the table

show create table 表名称\G

#查看student表的详细创建信息
show create table student\G
        Table: student
Create Table: CREATE TABLE `student` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

The above results show that the default character set of the student table is "latin1" which does not support Chinese.

9. View the creation information of the database

show create database 数据库名\G

#查看atguigudb数据库的详细创建信息
show create database atguigudb\G
#结果如下

*************************** 1. row ***************************
       Database: atguigudb

Create Database: CREATE DATABASE `atguigudb` /*!40100 DEFAULT CHARACTER SET latin1 */

1 row in set (0.00 sec)

The above results show that the atguigudb database does not support Chinese, and the default character set is latin1.

10. Delete table

drop table table name;

#删除学生表
drop table student;

11. Delete the database

drop database database name;

#删除atguigudb数据库
drop database atguigudb;

MySQL encoding settings

MySQL5.7

Problem reproduction: command line operation sql garbled problem

mysql> INSERT INTO t_stu VALUES(1,'张三','男');
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'sname' at 
row 1

problem solved

Step 1: View encoding commands

show variables like 'character_%';

show variables like 'collation_%';

Step 2: Modify the my.ini configuration file in the mysql data directory

default-character-set =utf8 #default character set

[mysqld] # About line 76, add below it

...

character-set-server=utf8

collation-server=utf8_general_ci

Note: It is recommended to use an advanced text editor such as notepad++ to modify the configuration file. Opening and modifying the configuration file with software such as Notepad may cause the file encoding to be changed to "including BOM header", and the service restart will fail. 

 Step 3: Restart the service

 Step 4: View encoding commands

show variables like 'character_%';

show variables like 'collation_%';

 

 

If the above configuration is correct. Then we can create a new database, create a new data table, and then add data containing Chinese.

MySQL8.0

Before MySQL 8.0, the default character set was latin1, and the utf8 character set pointed to utf8mb3. Website developers often modify the encoding to utf8 character set when designing the database. If you forget to modify the default encoding, there will be garbled characters. Starting from MySQL 8.0, the default encoding of the database is changed to utf8mb4, thus avoiding the above-mentioned garbled problem.


MySQL graphical management tool

MySQL graphical management tools greatly facilitate the operation and management of the database. Commonly used graphical management tools include: MySQL Workbench, phpMyAdmin, Navicat Premium, MySQLDumper, SQLyog, dbeaver, MySQL ODBC Connector.

Tool 1. MySQL Workbench

MySQL Workbench, a graphical management tool officially provided by MySQL, fully supports versions above MySQL 5.0. MySQL Workbench is divided into community edition and commercial edition. The community edition is completely free, while the commercial edition is charged annually.

MySQL Workbench provides visual design, model building, and database management functions for database administrators, program developers, and system planners. It includes tools for creating complex data modeling ER models, forward and reverse database engineering, and can also be used to perform documentation tasks that are often time-consuming and difficult to change and manage.

Download address: http://dev.mysql.com/downloads/workbench/.

use:

First, we click the "Start" button in the lower left corner of Windows. If you are a Win10 system, you can directly see all programs. Then, find "MySQL", click on it, and find "MySQL Workbench 8.0 CE". Click to open Workbench, as shown in the following figure:

There is a local connection in the lower left corner, click it, enter the Root password, and log in to the local MySQL database server, as shown in the following figure:

 

 

 This is a graphical interface, let me introduce you to this interface.

  • Above is the menu. The upper left is the navigation bar, where we can see the databases in the MySQL database server, including data tables, views, stored procedures, and functions; the lower left is the information bar, which can display information about the database, data tables and other objects selected above.
  • The upper middle is the work area, where you can write SQL statements, and click the third run button on the left of the upper menu bar to execute the SQL statements in the work area.
  • Below the middle is the output area, which is used to display the running status of the SQL statement, including when it started running, the running content, the running output, and the time spent.  

Tool 2. Navicat

Navicat MySQL is a powerful MySQL database server management and development tool. It can work with any MySQL version 3.21 or above, supports triggers, stored procedures, functions, events, views, manage users, etc., and is easy to learn and use for novices. Its well-designed graphical user interface (GUI) allows users to create, organize, access and share information quickly and easily in a safe and easy way. Navicat supports Chinese and is available in a free version.

Download address: http://www.navicat.com/.

 

Tool 3. SQLyog

SQLyog is a simple, efficient, and powerful graphical MySQL database management tool produced by the industry-renowned Webyog company. This tool is developed using C++ language. This tool can easily create databases, tables, views, and indexes, etc., and can also easily perform operations such as insert, update, and delete. At the same time, it can easily backup and restore databases and data tables. This tool can not only import and export a large number of files through SQL files, but also import and export data in various formats such as XML, HTML and CSV.

Download address: http://www.webyog.com/ , readers can also search for the download address of the Chinese version.

 

Tool 4: dbeaver 

DBeaver is a general-purpose database management tool and SQL client that supports all popular databases: MySQL, PostgreSQL, SQLite, Oracle, DB2, SQL Server, Sybase, MS Access, Teradata, Firebird, Apache Hive, Phoenix, Presto, etc. DBeaver is lighter than most SQL management tools and supports Chinese interface. As a free and open source product, DBeaver Community Edition is no less functional and usable than other similar software.

Download link: https://dbeaver.io/download/

 

 

 

Possible connection problems:

Some graphical interface tools, especially the old versions of the graphical interface tools, appear "Authentication plugin 'caching_sha2_password' cannot be loaded" error when connecting to MySQL8.

The reason for this is that the encryption rule in versions before MySQL8 is mysql_native_password, but after MySQL8, the encryption rule is caching_sha2_password. There are two ways to solve the problem. The first is to upgrade the GUI tool version, and the second is to restore the MySQL8 user login password encryption rules to mysql_native_password.

The second solution is as follows. After logging in to the MySQL database with the command line, execute the following command to modify the user password encryption rule and update the user password. Here, modify the user password rule for the user name "root@localhost" to "mysql_native_password", and the password value is "123456", as shown in the figure.

#使用mysql数据库
USE mysql; 

#修改'root'@'localhost'用户的密码规则和密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'abc123'; 

#刷新权限
FLUSH PRIVILEGES;

 


 MySQL directory structure and source code

main directory structure

MySQL source code acquisition

First, you have to enter the MySQL download interface. Here you do not choose to use the default "Microsoft Windows", but through the drop-down bar, find "Source Code", in the operating system version below, select Windows (Architecture Independent), and then click download.

Next, unzip the downloaded compressed file, and we get the source code of MySQL.

MySQL is developed with C++. Let me briefly introduce the composition of the source code.

Each subdirectory under the mysql-8.0.22 directory contains the source code of each component of MySQL:

  • The sql subdirectory is the MySQL core code;
  • The libmysql subdirectory is the client program API;
  • The mysql-test subdirectory is a test tool;
  • The mysys subdirectory is the operating system related functions and auxiliary functions;

The source code can be opened and viewed with Notepad. If you have a C++ development environment, you can also open and view it in the development environment.

 

As shown in the figure above, the source code is not mysterious, it is ordinary C++ code, the same as you are familiar with, and there are many comments to help you understand. Reading the source code is like talking to the developers of MySQL, which is very interesting.


Alright, this is the end of Xiao Yalan's content for today, let's continue to work hard! ! !

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/130536484
Recommended