天天看點

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

提示:以下操作均在root使用者下進行,如在普通使用者,請自行加上sudo!

# 檢視有沒有安裝MySQL:

dpkg -l | grep mysql

# 安裝MySQL:

apt install mysql-server

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

安裝完成之後可以使用如下指令來檢查是否安裝成功:

netstat -tap | grep mysql

通過上述指令檢查之後,如果看到有 mysql 的socket處于 LISTEN 狀态則表示安裝成功。

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

登入mysql資料庫可以通過如下指令:

mysql -u root -p

-u 表示選擇登陸的使用者名, -p 表示登陸的使用者密碼,現在是mysql資料庫是沒有密碼的,Enter password:處直接回車,就能夠進入mysql資料庫。

然後通過 show databases; 就可以檢視目前的所有資料庫。

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

接下來,為了確定資料庫的安全性和正常運轉,對資料庫進行初始化操作。這個初始化操作涉及下面5個步驟。

(1)安裝驗證密碼插件。

(2)設定root管理者在資料庫中的專有密碼。

(3)随後删除匿名賬戶,并使用root管理者從遠端登入資料庫,以確定資料庫上運作的業務的安全性。

(4)删除預設的測試資料庫,取消測試資料庫的一系列通路權限。

(5)重新整理授權清單,讓初始化的設定立即生效。

對于上述資料庫初始化的操作步驟,在下面的輸出資訊旁邊我做了簡單注釋。

[email protected]:~# 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:   # 輸入要為root管理者設定的資料庫密碼

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) : y    # 删除匿名賬戶

Success.

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    # 禁止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) : y  # 删除test資料庫并取消對它的通路權限

- Dropping test database...

Success.

- Removing privileges on test database...

Success.

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  # 重新整理授權表,讓初始化後的設定立即生效

Success.

All done!

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

檢查mysql服務狀态:

systemctl status mysql

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

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

再次用mysql -u root -p指令,Enter password:處輸入剛設定的密碼,回車,就能夠進入mysql資料庫。

使用 use mysql; 指令打開mysql命名的資料庫,顯示目前資料庫的表:show tables; 查詢user表裡的資料:select * from user;(user表裡是mysql資料庫的所有賬戶資訊)

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

現在配置mysql允許遠端通路,首先編輯 /etc/mysql/mysql.conf.d/mysqld.cnf 配置檔案:

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

注釋掉bind-address          = 127.0.0.1

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

儲存退出,然後進入mysql資料庫,執行授權指令:

mysql -u root -p

mysql> grant all on *.* to root@'%' identified by '你的密碼' with grant option;

mysql> flush privileges;    # 重新整理權限

mysql> exit

然後執行exit指令退出mysql服務,再執行如下指令重新開機mysql:

systemctl restart mysql

現在Windows下可以使用Navicat圖形化工具遠端連接配接Ubuntu下的MySQL資料庫,輸入剛授權遠端權限的密碼。

這裡順便再分享下:Navicat for MySQL 10.1.7安裝包

ubuntu安裝mysql_Ubuntu18.04下安裝MySQL

OK,到這裡Ubuntu18.04下安裝MySQL已經完成了。

操作過程中我遇到了以下錯誤:

E: 無法獲得鎖 /var/lib/dpkg/lock-frontend - open (11: 資源暫時不可用)

E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?