Ubuntu Server 20.04 安裝MySQL


1. 通過apt安裝MySQL

1 #命令1
2 sudo apt-get update
3 #命令2
4 sudo apt-get install mysql-server

2. 配置mysql初始化信息

1 sudo mysql_secure_installation

配置說明:

ubuntu@VM-0-10-ubuntu:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: N(選擇N,不會進行密碼的強校驗)
Please set the password for root here.

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : N(選擇N,允許root遠程連接)

 ... skipping.
By default, MySQL 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.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N(選擇N,不刪除test數據庫)

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y(選擇Y,修改權限立即生效)
Success.

All done! 

3. 創建數據庫

1 #1 創建數據庫TestDB
2 CREATE DATABASE TestDB;

4. 配置訪問權限

1 # 登陸mysql
2 sudo mysql -uroot -p
3 #切換數據庫
4 use mysql;
5 #查詢用戶表命令:
6 select User,Host from user;
 1 ubuntu@VM-0-10-ubuntu:~$ ubuntu@VM-0-10-ubuntu:~$ sudo mysql -u root -p
 2 Enter password:
 3 Welcome to the MySQL monitor.  Commands end with ; or \g.
 4 Your MySQL connection id is 29
 5 Server version: 8.0.21-0ubuntu0.20.04.4 (Ubuntu)
 6 
 7 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
 8 
 9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12 
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14 
15 mysql> use mysql;
16 Reading table information for completion of table and column names
17 You can turn off this feature to get a quicker startup with -A
18 
19 Database changed
20 mysql> select User,Host from user;
21 +------------------+-----------+
22 | User             | Host      |
23 +------------------+-----------+
24 | Joseph           | %         |
25 | debian-sys-maint | localhost |
26 | mysql.infoschema | localhost |
27 | mysql.session    | localhost |
28 | mysql.sys        | localhost |
29 | root             | localhost |
30 +------------------+-----------+
31 6 rows in set (0.00 sec)
32 
33 mysql>   

創建新用戶並賦予權限

1 # 創建用戶Joseph@'%',密碼是‘1234562 create user Joseph@'%' identified by '123456';
3 # 賦予其TestDB數據庫的遠程連接權限
4 grant all privileges on Joseph.* to TestDB;

查看MySQL端口號

1 mysql> show global variables like 'port';
2 +---------------+-------+
3 | Variable_name | Value |
4 +---------------+-------+
5 | port          | 3306  |
6 +---------------+-------+
7 1 row in set (0.00 sec)

退出MySQL查看3306端口是否正常

1 # 退出MySQL
2 mysql> exit
3 Bye
4 # 查看3306端口是否正常
5 ubuntu@VM-0-10-ubuntu:~$ netstat -an | grep 3306
6 tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
7 ubuntu@VM-0-10-ubuntu:~$   

注意:現在的3306端口綁定的IP地址是本地的127.0.0.1

修改Mysql配置文件(注意路徑)

1 ubuntu@VM-0-10-ubuntu:~$ vim /etc/mysql/mysql.conf.d/mysqld.cnf

找到

bind-address            = 127.0.0.1

前面加#注釋掉

5. MySQL服務命令

1 # 檢查服務狀態
2 sudo service mysql status
3 # 停止 MySQL 服務
4 sudo service mysql stop
5 # 啟動 MySQL 服務
6 sudo service mysql start

6. 騰訊雲或者阿里雲注意要配置安全組信息放開端口

7. 注意防火牆狀態

1 # 查看防火牆狀態
2 sudo ufw status
3 # 停止防火牆
4 sudo  ufw disable
5 # 啟動防火牆
6 sudo  ufw enable


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM