Ubuntu installs Mysql to enable remote connection

1. Install mysql with apt-get

#更新一下apt 仓库
sudo apt-get update


#安装mysql-service
sudo apt-get install mysql-server -y


#也可以指定版本   (可选)
sudo apt install mysql-server-5.7

2. Check whether the status of Mysql is successfully installed

sudo service mysql status

 3. Set root password

At this time, the root account of mysql does not have a password, so you can mysqllog in directly with the command:

$ mysql

Set the password of root ( mynewpasswordchange part of it to the password you want to set):

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';

Exit, enter mysqlthe command and find that you cannot log in directly:

So far you can use mysql directly on the server.

2. Set up remote connection

Edit the configuration file to listen for remote connections

By default, the MySQL database only listens to local connections. If we want to remotely connect to the database from the external network, we need to modify the configuration file so that MySQL can listen to the remote fixed ip or listen to all remote ips.
Here you need to use a command line text editor, I use vim so I will teach vim

1. Open  mysqld.cnf the configuration file with vim:

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

2. Find this line: bind-address = 127.0.0.1

127.0.0.1When this value is only listening to local connections, it 0.0.0.0can be changed to listen to all connections, or it can be changed to only allow specified ip connections.
Now vim is in the reading mode, click to ienter the editing mode, and then use the up, down, left and right keys to navigate to this line (when INSERT is displayed at the bottom, it indicates that it is in the editing mode, press Esc to exit and return to the reading mode):

After changing, press Esc to exit editing mode, then enter :wqsave and exit. ( :qExit without saving if entered)

3. Restart the mysql service to make the changes take effect:

sudo service mysql restart

Allow the root account to use remote connections

By default, mysql only allows the root account to be used locally. It needs to be modified to allow remote use of the root account (I haven't tried other accounts, but the principle is the same). Log in to mysql first:

mysql -u root -p

Enter your password and log in.
Then select the database:    mysql 

use mysql;

View the host permissions of the account:

select user, host from user;

hostlocalhostOnly local use is allowed, and it can be used remotely when changed to : 

update user set host='%' where user='root';

exit mysql.

exit;

Check the firewall status that comes with ubuntu

sudo ufw status

If it inactivemeans that the firewall is not turned on, then don't worry about it. What does the firewall do? My own understanding is that if the firewall is enabled, all ports on the server are forbidden to connect by default , and only the ports you allow are allowed to connect, similar to this:

So if the firewall is open, then either close the firewall directly, or add a rule to allow the firewall to release port 3306 (mysql's default port)::

#关闭防火墙
sudo ufw disable

#添加一条规则让防火墙放行3306端口
sudo ufw allow 3306

After setting the above operations, you can test whether you can connect to mysql remotely 


 

Guess you like

Origin blog.csdn.net/djklsajdklsajdlk/article/details/127521438