Ubuntu 安裝 MySQL

安裝 MySQL

下面我們有兩個由 && 分隔的命令。第一個命令將更新軟件包列表以確保可獲得 MySQL 的最新版本和依賴項。然後第二個命令將下載並安裝 MySQL。

$ sudo apt update && sudo apt install mysql-server

檢查 MySQL 服務是否正在運行

$ sudo service mysql status

首次設定密碼

login as: zabbix
zabbix@10.10.3.122's password:
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-43-generic x86_64)

zabbix@zabbix-test:~$ sudo mysql
[sudo] zabbix 的密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'p@ssw0rd';
Query OK, 0 rows affected (0.02 sec)

mysql> quit
Bye

zabbix@zabbix-test:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

變更 root 密碼

root 變更密碼後,可使用 mysqladmin 來強制變更密碼

$ mysqladmin -u root -p password 'p@ssw0rd'
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
zabbix@zabbix-test:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)

設定安全性(Security)

運行 mysql_secure_installation 為 MySQL Server 配置安全性。系統會詢問您是否要設定驗證密碼插件(VALIDATE PASSWORD PLUGIN)。除非想要強制執行嚴格的密碼規則,不然這並不是真正需要的。如果不想設置驗證密碼插件(VALIDATE PASSWORD PLUGIN),請在此處直接按 ENTER。

zabbix@zabbix-test:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

 ... skipping.
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) :

 ... 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) :

 ... 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) :

 ... 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) :

 ... skipping.
All done!
zabbix@zabbix-test:~$

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *