1. 程式人生 > 實用技巧 >Ubuntu18.04 安裝MySQL

Ubuntu18.04 安裝MySQL

環境資訊:

OS:Ubuntu18.04
MySQL: 5.7.22

安裝MYSQL

Ubuntu中,預設情況下,只有最新版本的MySQL包含在APT軟體包儲存庫中,要安裝它,只需更新伺服器上的包索引並安裝預設包apt-get

#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server

  

初始化配置

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

配置遠端訪問

在Ubuntu下MySQL預設是隻允許本地訪問的,使用客戶端連線工具是連不上的;
如果你要其他機器也能夠訪問的話,需要進行配置;

步驟如下:

sudo mysql -uroot -proot

grant all privileges on *.* to root@'%' identified by 'root';
flush privileges;

使用客戶端工具嘗試連線,如果連不上:

檢視3306埠是否開啟:

netstat -an|grep 3306

由此可見,3306埠只是在IP 127.0.0.1上監聽,所以拒絕了其他IP的訪問。

解決方法:

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 

把其中的bind-address= 127.0.0.1直接註釋掉。

重啟mysql服務

sudo service mysql restart

客戶端連線測試成功。