Software testing environment -- install MySQL on Linux (detailed), fine sorting

1, Foreword

Several common ways to install software on Linux:

  • Source code compilation
  • Decompress the compressed package (generally tar.gz)
  • Compiled installation package (RPM, DPKG, etc.)
  • Online installation (YUM, APT, etc.)
    The convenience of the above methods increases in turn, but the versatility decreases in turn

Delete the development header files and libraries of the old version of MySQL

rm -rf  /etc/my.cnfrm -rf  /var/lib/mysql

2, Install mysql

mysql can be installed using yum, which is more convenient

[root@yoyo ~]# cd /usr/local/
[root@yoyo ~]# mkdir mysql-community-release
[root@yoyo ~]# cd mysql-community-release
[root@yoyo ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# yum -y install mysql-community-server

Check the version number after installation: mysql -V

[root@yoyo local]# mysql -V
mysql  Ver 14.14 Distrib 5.6.42, for Linux (x86_64) using  EditLine wrapper

After installation, restart the mysql service, and check that the status is Active: active (running), indicating that the startup is successful

Get the software test materials free of charge - help modify and improve the resume and other questions (Q & A)

Start service: service mysqld restart

[root@yoyo local]# service mysqld restart

Check the running status of MySQL: systemctl status mysql service

[root@yoyo local]# systemctl status  mysql.service
[root@yoyo local]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

[root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-01-15 09:53:42 CST; 47s ago
 Main PID: 946 (mysqld_safe)
   CGroup: /system.slice/mysqld.service
           ├─ 946 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─1282 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/v...

Jan 15 09:53:39 yoyo systemd[1]: Starting MySQL Community Server...
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Logging to '/var/log/mysqld.log'.
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan 15 09:53:42 yoyo systemd[1]: Started MySQL Community Server.

3, mysql reset password

Method 1:
mysql is used for the initial installation. The root account does not have a password by default. The system will give a temporary password in / var / log / mysqld Log can be viewed

[root@yoyo local]# grep 'temporary password' /var/log/mysqld.log

As shown in the figure below, the temporary password appears. After copying it, you can log in to mysql

[root@yoyo local]# mysql -u root -p

See Enter password: enter the password, reset the password and exit to exit mysql

[root@yoyo local]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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.
# reset password 
mysql> update user set password = Password('root') where User = 'root';
# Execute after entering (refresh the tables related to MySQL system permissions)
mysql> flush privileges;
# Then execute exit to exit:
mysql> exit
Bye
[root@yoyo local]#

Method 2:
If the temporary password cannot be found in the previous step, use this method to stop mysql and start it in a safe way

Get the software test materials free of charge - help modify and improve the resume and other questions (Q & A)

[root@yoyo local]# systemctl stop  mysql.service

Start mysql safely:

[root@yoyo local]# /usr/bin/mysqld_safe —skip-grant-tables >/dev/null 2>&1 &

Then execute

[root@yoyo local]#  /usr/bin/mysql -u root mysql

After the "MySQL >" prompt appears, enter:

mysql> update user set password = Password('root') where User = 'root';

Execute after entering (refresh the tables related to MySQL system permissions):

mysql> flush privileges;

Then execute exit to exit:

mysql> exit;

After exiting, log in to mysql with the following command to see if it succeeds:

[root@yoyo local]#mysql -u root -p

Enter the password as prompted: root

[root@yoyo local]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

When Welcome to the MySQL appears, the login is successful

4, View mysql port number

The default port of mysql is 3306. How to check the msyql port number? You can log in with the root account and execute show variables like 'port'

[root@yoyo local]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

mysql>

5, Authorize mysql remote connection

After MySQL is installed on linux, in order to facilitate viewing, you can install a remote database connection client on the local computer and connect mysql remotely

Method 1:
First create a new root user with the login password of password, which can be named by yourself

mysql> create user 'root'@'%' identified by 'password';
[root@yoyo sysconfig]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> create user 'root'@'%' identified by 'password';  
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
mysql> exit

Method 2:
Check the user table and update the record with host as localhost and user as root to host% to allow remote access
When operating mysql, execute use mysql first

[root@yoyo sysconfig]# mysql -u root -p
Enter password: 
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
\Database changed
mysql> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user  | host      | password                                  |
+-------+-----------+-------------------------------------------+
| root  | yoyo      | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root  | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root  | ::1       | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
|       | yoyo      |                                           |
| root1 | %         | *668425423DB5193AF921380129F465A6425216D0 |
| root  | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

If you see the host corresponding to% after root, it indicates that you have remote access permission. If you display localhost, update it. How to flush privileges to refresh the system permission

mysql> use mysql
mysql> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user  | host      | password                                  |
+-------+-----------+-------------------------------------------+
| root  | yoyo      | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root  | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root  | ::1       | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
|       | yoyo      |                                           |
| root1 | %         | *668425423DB5193AF921380129F465A6425216D0 |
| root  | %         | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> exit

Method 3:

Authorization method, which gives the root user the right to log in remotely

# If you want root to connect to the mysql server from any host using 123456
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123546' WITH GRANT OPTION;
# If you want to allow user root to connect to the mysql server from the host with ip 192.168.1.3, and use 123456 as the password

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '123456' WITH GRANT OPTION;
Next, go to Alibaba cloud ECS Service background security group-Add rules, add 3306 port access rights, and use Navicat Remote tools can be connected

The account password here is the "root" and "password" set in method 1 above

Turn on and off services
1. Start mysql

service mysqld start

2. View mysql running status

service mysqld status     # Or systemctl status mysql service

3 stop mysql service

service mysqld stop       # Or systemctl stop mysql service

4 restart mysql

systemctl restart  mysql.service

5 view the running process

ps -ef | grep mysqld
[root@yoyo sysconfig]# ps -ef | grep mysql
mysql     2506     1  0 12:51 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     2674  2506  0 12:51 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root      2748  1668  0 12:55 pts/0    00:00:00 grep --color=auto mysql

6. View mysql port

netstat -tulpn |grep mysql
[root@yoyo sysconfig]# netstat -tulpn |grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      2674/mysql

6, Problems encountered

When starting mysql, Failed to start MySQL Community Server appears The specific error reports are as follows

[root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Mon 2019-01-14 20:31:27 CST; 13h ago
  Process: 26800 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
  Process: 26799 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
  Process: 26786 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 26799 (code=exited, status=1/FAILURE)

Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service holdoff time over, scheduling restart.
Jan 14 20:31:27 yoyo systemd[1]: start request repeated too quickly for mysqld.service
Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.

At first, I thought there was a problem with the configuration. Later, baidu searched and reboot ed to restart the linux server

Tags: Linux Database MySQL unit testing software testing Testing

Posted by parkej60 on Wed, 18 May 2022 02:16:56 +0300