Ubuntu18.04下安裝MySQL

提示:如下操做均在root權限下進行。mysql

# 查看有沒有安裝MySQL:linux

dpkg -l | grep mysqlsql

# 安裝MySQL:數據庫

apt install mysql-serverubuntu

安裝完成以後可使用以下命令來檢查是否安裝成功:vim

netstat -tap | grep mysql安全

經過上述命令檢查以後,若是看到有 mysql 的socket處於 LISTEN 狀態則表示安裝成功。frontend

登陸mysql數據庫能夠經過以下命令:socket

mysql -u root -pide

-u 表示選擇登錄的用戶名, -p 表示登錄的用戶密碼,如今是mysql數據庫是沒有密碼的,Enter password:處直接回車,就可以進入mysql數據庫。

而後經過 show databases; 就能夠查看當前的全部數據庫。

接下來,爲了確保數據庫的安全性和正常運轉,對數據庫進行初始化操做。這個初始化操做涉及下面5個步驟。

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

(2)設置root管理員在數據庫中的專有密碼。

(3)隨後刪除匿名帳戶,並使用root管理員從遠程登陸數據庫,以確保數據庫上運行的業務的安全性。

(4)刪除默認的測試數據庫,取消測試數據庫的一系列訪問權限。

(5)刷新受權列表,讓初始化的設定當即生效。

對於上述數據庫初始化的操做步驟,在下面的輸出信息旁邊我作了簡單註釋。

root@ubuntu-virtual-machine:~# 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!

檢查mysql服務狀態:

systemctl status mysql

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

再次用mysql -u root -p命令,Enter password:處輸入剛設置的密碼,回車,就可以進入mysql數據庫。

使用 use mysql; 命令打開mysql命名的數據庫,顯示當前數據庫的表:show tables; 查詢user表裏的數據:select * from user;(user表裏是mysql數據庫的全部帳戶信息)

如今配置mysql容許遠程訪問,首先編輯 /etc/mysql/mysql.conf.d/mysqld.cnf 配置文件:

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

註釋掉bind-address          = 127.0.0.1

保存退出,而後進入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安裝包

連接:https://pan.baidu.com/s/1CyDXr79v7Ufc60fv_XQ_gA ,提取碼:ohf8

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?

解決方法,參考自文章:https://www.linuxidc.com/Linux/2018-12/155949.htm

相關文章
相關標籤/搜索