一、安裝MySQL
在Ubuntu
中,默認情況下,只有最新版本的MySQL
包含在APT
軟件包存儲庫中,要安裝它,只需更新服務器上的包索引並安裝默認包apt-get
。
sudo apt-get update
1、安裝mysql服務:
sudo apt install mysql-server-5.7
2、檢查狀態:
需要先安裝net-tools
sudo apt install net-tools
sudo netstat -tap | grep mysql
3、注意查看mysql版本使用:
mysql -V
4、查看MySQL5.7默認賬號和密碼:
sudo cat /etc/mysql/debian.cnf
二、配置MySQL
sudo mysql_secure_installation
#1 VALIDATE PASSWORD PLUGIN can be used to test passwords... Press y|Y for Yes, any other key for No: N (我的選項) #2 Please set the password for root here... New password: (輸入密碼) Re-enter new password: (重復輸入) #3 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) : N (我的選項) #4 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 (我的選項) #5 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) : N (我的選項) #6 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 (我的選項)
三、查mysql服務狀態
systemctl status mysql.service
顯示如下結果說明mysql服務是正常的:
四、修改root賬戶秘密認證方式:
連接到Mysql:
sudo mysql -uroot -p
1、查看用戶:
mysql> select user, plugin from mysql.user;
2、重置Root密碼,修改認證方式:
mysql> update mysql.user set authentication_string=PASSWORD('123456'), plugin='mysql_native_password' where user='root'; mysql>flush privileges; mysql> exit
五、配置遠程訪問mysql:
1、修改配置文件,注釋掉bind-address = 127.0.0.1
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
2、保存退出,然后進入mysql服務
mysql -uroot -p
3、執行授權命令:
mysql>grant all ON *.* to root@'%' identified by '123456' with grant option; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
其中root@%
,localhost
就是本地訪問,配置成%
就是所有主機都可連接;第二個'123456'
為你給新增權限用戶設置的密碼。
4、重啟
sudo /etc/init.d/mysql restart
六、刪除MySQL
想安裝MySQL 8.0或者重裝MySQL 5.7的前提條件,可以先刪除已有的。
1.刪除 mysql:
sudo apt autoremove --purge mysql-server-* sudo apt remove mysql-server sudo apt autoremove mysql-server sudo apt remove mysql-common
2.清理殘留數據
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
七、安裝mysql可視化工具mysql-workbench
也可以更新軟件包索引並直接安裝 MySQL Workbench 軟件包:
sudo apt update
sudo apt install mysql-workbench
啟動 MySQL Workbench。
您可以通過鍵入 mysql-workbench
或單擊 MySQL Workbench 圖標 (Activities -> MySQL Workbench
) 從命令行啟動它。
當您第一次啟動 MySQL Workbench 時,應出現如下窗口:
連接配置界面
要添加新連接,請單擊 “MySQL Connections” 旁邊帶圓圈的加號⊕。
將打開一個新窗口 “Setup New Connection form”。在此示例中,我們將通過 SSH 連接到遠程服務器。在“Connection name”字段中輸入有意義的名稱,然后 Standard TCP/IP over SSH
從“連接方法”下拉列表中進行選擇。
- 在 “SSH Hostname”中,輸入服務器的主機名或 IP 地址,然后輸入 SSH 端口。
- 輸入您的遠程 “SSH Username”。對於身份驗證,您可以使用用戶密碼或 SSH 密鑰。
- 保留 “MySQL Hostname”字段的默認值 (127.0.0.1) 。
- 在“Username”和“Password”字段中輸入遠程數據庫登錄憑據。
完成后,單擊“測試連接”按鈕。
這里直接點擊已有的連接:
配置完成后,在主界面選擇數據庫進行連接: