Ubuntu下安裝MySQL(阿里雲服務器)


安裝客戶端和依賴環境

sudo apt install mysql-server #安裝mysql服務器端
sudo apt install mysql-client #安裝mysql客戶端
sudo apt install libmysqlclient-dev #安裝服務端/客戶端依賴環境(可有可無,建議安裝)

初始化MySQL(MySQL 5.7並未設置默認密碼,需要初始化,即運行安全向導)

sudo mysql_secure_installation

安全向導:

1.(是否設置隨機密碼,Y/y隨機密碼, N/n自己輸入密碼)

VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的選項)
Please set the password for root here...
New password: (輸入密碼)
Re-enter new password: (重復輸入)

2.(是否刪除匿名用戶,建議刪除)

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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y (我的選項)

3.(是否禁止root賬戶遠程登陸,建議禁止)

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 (我的選項)

4.(是否刪除test數據庫,建議刪除)

By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y (我的選項)

5.(是否重新加載權限表,應該重新加載)

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 (我的選項)

修改配置文件(具體的看是否需要遠程訪問數據庫)

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
/bind #查找bind-address,然后注釋該行並保存

重啟MySQL服務

sudo service mysql restart

從MySQL-Client登陸MySQL(首次登陸建議以root用戶身份登陸)

mysql -u root -p

創建用來遠程登陸的用戶

create user 用戶名(非root用戶)

為新建立的用戶賦予相應權限

grant all privileges on *.* to  'username'@'%' identified by 'password' with grant option;
# grant 修改用戶權限的關鍵字
# all(增、刪、改、查、創建數據庫和表等,除grant外的所有權限)
# on 后面跟數據庫名.表名(*代表所有)
# to 'username'@'ip'(%代表所有IP均可訪問該數據庫)
# identified by 'password'
# 關鍵字:privileges、with grant option

必須使用flush privileges的兩種情況

1、改密碼。

2、授權超用戶。

flush privileges 命令本質上的作用是將當前user和privilige表中的用戶信息/權限設置從mysql庫(MySQL數據庫的內置庫)中提取到內存里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM