查看倉庫版本
sudo apt-cache show mysql-server
安裝命令
sudo apt install mysql-server mysql-client mysql-common
運(yùn)行安全腳本
sudo mysql_secure_installation
第一步 提示是否安裝驗(yàn)證插件
# 安全的MySQL服務(wù)器部署
Securing the MySQL server deployment.
# 使用空白密碼連接到MySQL
Connecting to MySQL using a blank password.
# VALIDATE PASSWORD PLUGIN可用于測(cè)試密碼并提高安全性
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security.
# 它檢查密碼的強(qiáng)度址否,并允許用戶只設(shè)置那些足夠安全的密碼。
It checks the strength of password
and allows the users to set only those passwords which are
secure enough.
# 你想設(shè)置VALIDATE PASSWORD插件嗎
Would you like to setup VALIDATE PASSWORD plugin?
# 按y | Y表示是梧躺,任何其他鍵為否
Press y|Y for Yes, any other key for No:
這里我選擇不安裝:N
第二步 設(shè)置root密碼
Please set the password for root here.
New password:
Re-enter new password:
第三步 處理匿名賬戶
# 默認(rèn)情況下仔戈,MySQL安裝有一個(gè)匿名用戶,
# 允許任何人登錄MySQL而不必?fù)碛袨樗麄儎?chuàng)建的用戶帳戶婉称。
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.
# 這僅用于測(cè)試睛挚,并使安裝更加順利
This is intended only for testing, and to make the installation go a bit smoother.
# 您應(yīng)該在進(jìn)入生產(chǎn)環(huán)境之前將其刪除
You should remove them before moving into a production environment.
# 刪除匿名用戶靠汁? (按y | Y表示是,任何其他鍵表示否):
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
這里我選擇Y
第四步 是否允許root管理員從遠(yuǎn)程登錄
# 通常皆疹,只允許root連接'localhost'
Normally, root should only be allowed to connect from 'localhost'.
# 這可以確保別人無法猜測(cè)來自網(wǎng)絡(luò)的root密碼疏橄。
This ensures that someone cannot guess at the root password from the network.
# 禁止遠(yuǎn)程登錄? (按y | Y表示是略就,任何其他鍵表示否):
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
這里我選擇N软族,不禁止
第五步 刪除測(cè)試數(shù)據(jù)庫
# 默認(rèn)情況下,MySQL附帶一個(gè)名為'test'的數(shù)據(jù)庫任何人都可以訪問
By default, MySQL comes with a database named 'test' that anyone can access.
# 這也僅用于測(cè)試残制,應(yīng)在進(jìn)入生產(chǎn)環(huán)境之前刪除立砸。
This is also intended only for testing,
and should be removed before moving into a production environment.
# 刪除測(cè)試數(shù)據(jù)庫并訪問它? (按y | Y表示是初茶,任何其他鍵表示否):
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
這里我選擇Y
第六步 重新加載權(quán)限表
# 重新加載權(quán)限表將確保到目前為止所做的所有更改都將立即生效
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
# 現(xiàn)在重新加載權(quán)限表颗祝? (按y | Y表示是,任何其他鍵表示否):
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
這里我選擇YES
查看mysql服務(wù)狀態(tài)
systemctl status mysql
顯示如下就OK
image.png
開啟遠(yuǎn)程訪問
1 打開配置
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
2 注釋43行 bing-address = 127.0.0.1
授權(quán)root及設(shè)置密碼
# 授權(quán)所有庫和表的所有操作并設(shè)置密碼
mysql> grant all on *.* to root@'%' identified by '密碼' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新權(quán)限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退款
mysql> quit;
Bye
# 重啟Mysql
kong@kabob:~$ sudo systemctl restart mysql
查看Mysql字符編碼
命令:SHOW VARIABLES WHERE Variable_name LIKE 'character%' OR Variable_name LIKE 'collation%';
image.png
設(shè)置默認(rèn)字符編碼
# 打開配置
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
增加如下內(nèi)容
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
init_connect = ’SET NAMES utf8mb4’