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 mysql
log in directly with the command:
$ mysql
Set the password of root ( mynewpassword
change part of it to the password you want to set):
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';
Exit, enter mysql
the 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.1
When this value is only listening to local connections, it0.0.0.0
can 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 toi
enter 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
:wq
save and exit. (:q
Exit 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;
host
localhost
Only 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
inactive
means 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