一、安裝MySQL
- 執行如下命令
之後會有個提示輸入:y。如下apt install mysql-server
- 安裝完成之後輸入以下命令查看是否成功
顯示LISTEN則代表安裝成功netstat -tap | grep mysql
二、配置MySQL
-
此時可以輸入以下命令直接進入mysql,沒有密碼
mysql -u root -p
輸入密碼出直接回車,可以查看所有數據庫
-
接下來退出mysql,執行以下命令對mysql進行配置
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允許遠程訪問,編輯以下文件
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
如下:
重啓mysql:
systemctl restart mysql