Ubuntu 安裝 mysql

守望者与托养者發表於2024-08-07

Ubuntu 安裝 mysql

參考教程:

Ubuntu18.04 安裝MySQL

1. 安裝 MySQL

Ubuntu中,預設情況下,只有最新版本的MySQL包含在APT軟體包儲存庫中,要安裝它,只需更新伺服器上的包索引並安裝預設包apt-get

(base) simulate@simulate-VirtualBox:~$ sudo apt-get update

(base) simulate@simulate-VirtualBox:~$ sudo apt-get install mysql-server

2. 配置 MySQL

2.1 初始化配置

(base) simulate@simulate-VirtualBox:~$ sudo mysql_secure_installation

配置項較多,如下所示:

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的選項)

#2
Please set the password for root here...
New password: (輸入密碼:666)
Re-enter new password: (重複輸入:666)

#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的選項)

#4
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) : Y (我的選項)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的選項)

#6
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 (我的選項)

2.2 檢查 mysql 服務狀態

(base) simulate@simulate-VirtualBox:~$ systemctl status mysql.service

顯示如下結果說明mysql服務是正常的:

3. 配置遠端訪問

UbuntuMySQL預設是隻允許本地訪問的,使用workbench連線工具是連不上的;

如果你要其他機器也能夠訪問的話,需要進行配置;

(base) simulate@simulate-VirtualBox:~$ sudo mysql -uroot -p

登入root進行其他設定:

設定使用者名稱:root,密碼:666

mysql> update mysql.user set authentication_string=PASSWORD('666'), plugin='mysql_native_password' where user='root';

mysql> create user 'root'@'%' identified by '666';

mysql> grant all on *.* to 'root'@'%';

mysql> flush privileges;

相關文章