table of Contents
1. Build MySQL database under Linux
4. Log in to MySQL and modify the root password
1. Build MySQL database under Linux
1. MySQL database
MySQL is a relational database management system developed by the Swedish company MySQL AB and is a product of Oracle. MySQL is one of the most popular relational database management systems. Relational databases store data in different tables instead of putting all data in a large warehouse, which increases speed and flexibility.
MySQL has two engines: MyISAM and innoDB. The difference is that the MyISAM type table emphasizes performance, and its execution speed is faster than the InnoDB type, but it does not provide transaction support. If a large number of SELECT operations are performed, MyISAM is a better choice and supports table locks. InnoDB provides advanced database functions such as transaction support for transactional external keys, and performs a large number of INSERT or UPDATE. For performance reasons, InnoDB tables should be used to support row locks.
2. MySQL installation
(1) Command line installation (none)
(2) Download and install the source code
Checked the current installation method is rpm installation, the download path is as follows: https://dev.mysql.com/downloads/mysql/
[root@localhost opt]# tar -xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle\ \(1\).tar
mysql-community-common-8.0.21-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-8.0.21-1.el7.x86_64.rpm
mysql-community-devel-8.0.21-1.el7.x86_64.rpm
mysql-community-server-8.0.21-1.el7.x86_64.rpm
mysql-community-client-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-test-8.0.21-1.el7.x86_64.rpm
(3) RPM installation
There are four main mysql components used here: server, client, common, libs, the order of installation is: common→libs→client→server
[root@localhost opt]# rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-common-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-8.0.21-1.e################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-8.0.21-1.el7################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-client-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-8.0.21-1.e################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-server-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-8.0.21-1.e################################# [100%]
Note:
(1) An error was reported when installing lib, the solution: yum remove mysql-libs, clear the previous dependencies and then continue the installation
[root@localhost opt]# rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.21-1.el7.x86_64
(2) An error is reported when installing the server, you need to install perl first
[root@localhost opt]# rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm
warning: mysql-community-server-8.0.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
/usr/bin/perl is needed by mysql-community-server-8.0.21-1.el7.x86_64
net-tools is needed by mysql-community-server-8.0.21-1.el7.x86_64
perl(Getopt::Long) is needed by mysql-community-server-8.0.21-1.el7.x86_64
wget https://cpan.metacpan.org/authors/id/S/SH/SHAY/perl-5.30.2.tar.gz
tar -zxvf perl-5.30.2.tar.gz -C /opt
./Configure -des -Dprefix=/opt/perl-5.30.2
make && make test
make install
perl -v
yum install net-tools
3. Start the mysql service
systemctl start mysqld.service //Start mysql
systemctl status mysqld.service //Check mysql status
systemctl stop mysqld.service //Close mysql
[root@localhost etc]# systemctl start mysqld.service
[root@localhost etc]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-10-04 16:39:16 EDT; 28s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 18727 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 18800 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─18800 /usr/sbin/mysqld
Oct 04 16:38:55 localhost.localdomain systemd[1]: Starting MySQL Server...
Oct 04 16:39:16 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost etc]# systemctl stop mysqld.service
After startup, you can view the service status through commands
[root@localhost ~]# ps -ef|grep mysql
mysql 18800 1 1 16:39 ? 00:00:51 /usr/sbin/mysqld
root 19229 18899 0 17:43 pts/1 00:00:00 grep --color=auto mysql
[root@localhost ~]# ps -ef|grep mysqld
mysql 18800 1 1 16:39 ? 00:00:51 /usr/sbin/mysqld
root 19231 18899 0 17:43 pts/1 00:00:00 grep --color=auto mysqld
[root@localhost ~]# netstat -anop|grep 3306
tcp6 0 0 :::33060 :::* LISTEN 18800/mysqld off (0.00/0/0)
tcp6 0 0 :::3306 :::* LISTEN 18800/mysqld off (0.00/0/0)
4. Log in to MySQL and modify the root password
In the new version of mysql, a default password is automatically set during the installation process. This password can be viewed through the log. The command is as follows
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2020-10-04T20:39:03.831989Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: pp4,WVoe?kOj
Here pp4,WVoe?kOj is the login password, use this password to log in, the login command is as follows:
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
You can modify the password after logging in. The first modification needs to be modified according to the complexity, otherwise an error will be reported. The modification command is as follows
mysql> ALTER USER root@localhost IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> ALTER USER root@localhost IDENTIFIED BY '123qweRTY!@#';
Query OK, 0 rows affected (0.05 sec)
After changing the password, you can view the related parameters of the password setting, the command is as follows
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.02 sec)
The password complexity is related to the parameter validate_password_policy. The parameter description is as follows. The default complexity is 1 or MEDIUM, so the password must contain numbers, lowercase or uppercase letters, and special characters for the first time .
Policy | Tests Performe (required |
---|---|
0 or LOW | Length |
1 or MEDIUM | numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters |
The default configuration of the password can be changed after the password is changed for the first time, which is convenient for setting a simple password
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 4 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
mysql> ALTER USER root@localhost IDENTIFIED BY '1234';
Query OK, 0 rows affected (0.06 sec)
5. Simple SQL operation
mysql> create database test_db;
Query OK, 1 row affected (0.05 sec)
mysql> use test_db;
Database changed
mysql> show tables;
Empty set (0.01 sec)
mysql> create table test01(id varchar(20),name varchar(20));
Query OK, 0 rows affected (0.12 sec)
mysql> insert into test01 values("test01", "yangxiaoyang");
Query OK, 1 row affected (0.10 sec)
mysql> select * from test01;
+--------+--------------+
| id | name |
+--------+--------------+
| test01 | yangxiaoyang |
+--------+--------------+
1 row in set (0.00 sec)
mysql> desc test01;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | varchar(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
Grant permissions to new users. Note that the authorization method of mysql8.0 and later versions has changed. This version separates account creation and authorization
mysql> create user 'yang'@'192.168.56.102' identified by '123456';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all privileges on *.* to 'yang'@'192.168.56.102' with grant option;
Query OK, 0 rows affected (0.13 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)
MySQL backup and restore
[root@localhost ~]# mysqldump -uroot -p1234 test_db > test_db.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
anaconda-ks.cfg data test_db.sql tmp
[root@localhost ~]# mysql -uroot -p1234 test_db < test_db.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
Reference documents: