阿里云百科分享在阿里云Linux服務(wù)器上部署MySQL數(shù)據(jù)庫(kù)流程,MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),常用于LAMP和LNMP等網(wǎng)站場(chǎng)景中。本教程介紹如何在Linux系統(tǒng)ECS實(shí)例上安裝、配置以及遠(yuǎn)程訪(fǎng)問(wèn)MySQL數(shù)據(jù)庫(kù)。
目錄
背景信息
Alibaba Cloud Linux 2/3、CentOS 7.x
步驟一:安裝MySQL
步驟二:配置MySQL
步驟三:遠(yuǎn)程訪(fǎng)問(wèn)MySQL數(shù)據(jù)庫(kù)
背景信息
本教程中的MySQL版本僅為示例,您在實(shí)際操作時(shí),MySQL的版本可能因軟件源的更新而有所不同。關(guān)于MySQL相關(guān)安裝路徑說(shuō)明如下:
-
配置文件:/etc/my.cnf
-
數(shù)據(jù)存儲(chǔ):/var/lib/mysql
-
命令文件:/usr/bin和/usr/sbin
-
ECS云服務(wù)器:aliyunbaike.com/go/ecs
-
操作系統(tǒng):CentOS 7.x、Alibaba Cloud Linux 2、Alibaba Cloud Linux 3。
-
實(shí)例安全組的入方向規(guī)則已放行22、80、443、3306端口。具體操作,請(qǐng)參見(jiàn)添加安全組規(guī)則。
Alibaba Cloud Linux 2/3、CentOS 7.x
步驟一:安裝MySQL
- 遠(yuǎn)程連接ECS實(shí)例。
具體操作,請(qǐng)參見(jiàn)使用VNC登錄實(shí)例。
-
運(yùn)行以下命令,更新YUM源。
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
-
運(yùn)行以下命令,安裝MySQL。
sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
-
運(yùn)行以下命令,查看MySQL版本號(hào)。
mysql -V
返回結(jié)果如下,表示MySQL安裝成功。
mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
?
步驟二:配置MySQL
-
運(yùn)行以下命令,啟動(dòng)并設(shè)置開(kāi)機(jī)自啟動(dòng)MySQL服務(wù)。
sudo systemctl start mysqld sudo systemctl enable mysqld
-
運(yùn)行以下命令,獲取并記錄root用戶(hù)的初始密碼。
sudo grep 'temporary password' /var/log/mysqld.log
執(zhí)行?命令結(jié)果示例如下。
說(shuō)明?2022-02-14T09:27:18.470008Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r_V&f2wyu_vI
示例末尾的
r_V&f2wyu_vI
為初始密碼,后續(xù)在對(duì)MySQL進(jìn)行安全性配置時(shí),需要使用該初始密碼。 -
運(yùn)行以下命令,對(duì)MySQL進(jìn)行安全性配置。
sudo mysql_secure_installation
-
根據(jù)提示信息,重置MySQL數(shù)據(jù)庫(kù)root用戶(hù)的密碼。
說(shuō)明?在輸入密碼時(shí),系統(tǒng)為了最大限度的保證數(shù)據(jù)安全,命令行將不做任何回顯。您只需要輸入正確的密碼信息,然后按Enter鍵即可。
Enter password for user root: #輸入已獲取的root用戶(hù)初始密碼 The existing password for the user account root has expired. Please set a new password. New password: #輸入新的MySQL密碼 Re-enter new password: #重復(fù)輸入新的MySQL密碼 The 'validate_password' component is installed on the server. The subsequent steps will run with the existing configuration of the component. Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #輸入Y選擇更新MySQL密碼。您也可以輸入N不再更新MySQL密碼。 New password: #輸入新的MySQL密碼 Re-enter new password: #重復(fù)輸入新的MySQL密碼 Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #輸入Y確認(rèn)使用已設(shè)置的密碼。
-
根據(jù)提示信息,刪除匿名用戶(hù)。
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 #輸入Y刪除MySQL默認(rèn)的匿名用戶(hù)。 Success.
-
禁止root賬號(hào)遠(yuǎn)程登錄。
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 #輸入Y禁止root遠(yuǎn)程登錄。 Success.
-
刪除test庫(kù)以及對(duì)test庫(kù)的訪(fǎng)問(wèn)權(quán)限。
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 #輸入Y刪除test庫(kù)以及對(duì)test庫(kù)的訪(fǎng)問(wèn)權(quán)限。 - Dropping test database... Success. - Removing privileges on test database... Success.
-
重新加載授權(quán)表。
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 #輸入Y重新加載授權(quán)表。 Success. All done!
-
步驟三:遠(yuǎn)程訪(fǎng)問(wèn)MySQL數(shù)據(jù)庫(kù)
建議您使用非root賬號(hào)遠(yuǎn)程登錄MySQL數(shù)據(jù)庫(kù)。下文示例中,將創(chuàng)建新的MySQL用戶(hù)賬戶(hù),用于遠(yuǎn)程訪(fǎng)問(wèn)MySQL。
- 遠(yuǎn)程連接ECS實(shí)例。
關(guān)于連接方式的介紹,請(qǐng)參見(jiàn)連接方式概述。
-
運(yùn)行以下命令后,輸入root用戶(hù)的密碼登錄MySQL。
sudo mysql -uroot -p
-
依次運(yùn)行以下命令,創(chuàng)建遠(yuǎn)程登錄MySQL的賬號(hào),并允許遠(yuǎn)程主機(jī)使用該賬號(hào)訪(fǎng)問(wèn)MySQL。
本示例賬號(hào)為
重要?dmsTest
、密碼為Ecs@123****
。實(shí)際創(chuàng)建賬號(hào)時(shí),需將示例密碼
Ecs@123****
更換為符合要求的密碼,并妥善保存。密碼要求:長(zhǎng)度為8至30個(gè)字符,必須同時(shí)包含大小寫(xiě)英文字母、數(shù)字和特殊符號(hào)??梢允褂靡韵绿厥夥?hào):()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
#創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)dmsTest,并授予遠(yuǎn)程連接權(quán)限。 create user 'dmsTest'@'%' identified by 'Ecs@123****'; #為dmsTest用戶(hù)授權(quán)數(shù)據(jù)庫(kù)所有權(quán)限。 grant all privileges on *.* to 'dmsTest'@'%'; #刷新權(quán)限。 flush privileges;
-
執(zhí)行以下命令,退出數(shù)據(jù)庫(kù)。
exit
-
使用
dmsTest
賬號(hào)遠(yuǎn)程登錄MySQL。-
(推薦)您可以通過(guò)阿里云提供的數(shù)據(jù)管理服務(wù)DMS(Data Management Service)來(lái)遠(yuǎn)程訪(fǎng)問(wèn)MySQL數(shù)據(jù)庫(kù)。具體操作,請(qǐng)參見(jiàn)云數(shù)據(jù)庫(kù)錄入。
-
您可以通過(guò)MySQL客戶(hù)端遠(yuǎn)程登錄MySQL進(jìn)行測(cè)試。例如:MySQL Workbench、Navicat。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-649223.html
-
? 本文源自阿里云官方文檔。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-649223.html
到了這里,關(guān)于在阿里云Linux服務(wù)器上部署MySQL數(shù)據(jù)庫(kù)流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!