Set up the MySQL environment of linux and configure remote login at the same time

MySQL is the most popular relational database management system. In terms of web applications, MySQL is one of the best RDBMS (Relational Database Management System) application software.

In addition, you can use MariaDB instead. The MariaDB database management system is a branch of MySQL, which is mainly maintained by the open source community and is licensed under GPL. One of the reasons for the development of this branch is that after Oracle acquired MySQL, there is a potential risk of closing MySQL to the source, so the community uses branching to avoid this risk.
The purpose of MariaDB is to be fully compatible with MySQL, including API and command line, so that it can easily become a substitute for MySQL.

Windows environment can be tested with HeidiSQL. https://downloads.mariadb.org/mariadb/
database operation, see https://www.runoob.com/mysql/mysql-tutorial.html for details

  • Install nariadb
sudo apt-get install mariadb-server

If the installation fails, it may be caused by the previous installation, but it was not uninstalled cleanly. Uninstall completely and reinstall again.

  • Clear mysql completely

First delete mysql:

sudo apt-get remove mysql-*
sudo apt-get remove mariadb-*

Then clean up the remaining data

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

It will pop up a dialog box, just choose yes

  • After the installation is successful, the configuration file is as follows
eric@eric-PC:/etc/mysql$ tree
.
├── conf.d
│   ├── mysql.cnf
│   └── mysqldump.cnf                                                                                                                                                                                   
├── debian.cnf                                                                                                                                                                                          
├── debian-start                                                                                                                                                                                        
├── mariadb.cnf                                                                                                                                                                                         
├── mariadb.conf.d                                                                                                                                                                                      
│   ├── 50-client.cnf
│   ├── 50-mysql-clients.cnf
│   ├── 50-mysqld_safe.cnf
│   └── 50-server.cnf
├── my.cnf -> /etc/alternatives/my.cnf
└── my.cnf.fallback

2 directories, 11 files

  • Start, stop, and restart services
service mysql start
service mysql stop
service mysql restart
  • Log in for the first time, use the command to change the password
 mysqladmin -u root password "1234";
  • Or log in without password in sudo mode, then change the password

Sudo passwordless login for the first time

eric@eric-PC:/$ sudo mysql
请输入密码
[sudo] eric 的密码:
验证成功
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 20

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> 

Query user

MariaDB [(none)]> 
MariaDB [(none)]> select User, host from mysql.user;
+------+-----------+
| User | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.000 sec)
MariaDB [(none)]> 

For root user, set a password of 1234, and exit after refreshing

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '1234' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> 
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
                                                                                                                                                                                                        
MariaDB [(none)]>                                                                                                                                                                                       
MariaDB [(none)]> exit                                                                                                                                                                                  
Bye 

Test non-sudo password login

eric@eric-PC:/$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 44
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 20

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

login successful!

  • At the same time, you can also run the database initialization script to set
eric@eric-PC:/$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
#键入数据库root用户密码(非linux用户密码),首次无密码直接回车
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.
#修改密码
Change the root password? [Y/n] y
New password: 新密码
Re-enter new password: 重复新密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
#是否禁止匿名登录
Remove anonymous users? [Y/n] n
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
#是否进制远程登录
Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
#是否删除test测试数据库
Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
#是否立即生效
Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

  • Remote login settings
    Check the binding of port 3306, if the following prompt appears, 127.0.0.1:3306, it means that only the local port is bound
eric@eric-PC:/$ netstat -an | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN     
eric@eric-PC:/$ 

Modify the file /etc/mysql/mariadb.conf.d/vim 50-server.cnf

eric@eric-PC:/etc/mysql/mariadb.conf.d$ sudo vim 50-server.cnf

Block bind-address = 127.0.0.1

#bind-address            = 127.0.0.1

Restart the service and check the binding of port 3306.

eric@eric-PC:/$ service mysql restart
eric@eric-PC:/$ 
eric@eric-PC:/$ netstat -an | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN     
eric@eric-PC:/$ 

Query user login permissions, root | localhost, indicating that the root user can only log in locally

MariaDB [(none)]> select User, host from mysql.user;
+------+-----------+
| User | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.001 sec)

Modify the root user's remote login authority,'root'@'192.168.16.%' means that only 192.168.16.xx address domain access is allowed, and'root'@'%' allows all ip domain access, and the access password is set to 1234

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.16.%' IDENTIFIED BY '1234' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]>

Remote login test

eric@eric-PC:/$ mysql -h 192.168.16.107 -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.3.23-MariaDB-0+deb10u1 Debian 20

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

login successful

  • Windows remote login
    Insert picture description here

Guess you like

Origin blog.csdn.net/pyt1234567890/article/details/112251226