Ubuntu19.04 安裝 MySQL 8.0.16

Ubuntu19.04 安裝 MySQL 8.0.16
環境信息:
OS:Ubuntu19.04
OpenJDK:8
MySQL: 8.0.16

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

1. 添加 MySQL APT Repository
MySQL APT repository 添加至系統的軟件倉庫列表裏,需要下載mysql-apt-config包;
下載頁面:https://dev.mysql.com/downloads/file/?id=487007

點擊安裝後進行2的步驟。

2.通過apt 安裝MySQL
#命令1

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

 

3. 配置MySQL
3.1 配置初始化信息

sudo mysql_secure_installation

 

配置說明:

weison@ubuntu19-04:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: N(選擇N,不會進行密碼的強校驗)
Please set the password for root here.

New password: 

Re-enter new password: 
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) : N(選擇N,不刪除匿名用戶)

 ... skipping.


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(選擇N,允許root遠程連接)

 ... skipping.
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) : N(選擇N,不刪除test數據庫)

 ... skipping.
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,修改權限立即生效)
Success.

All done! 

 

3.2 配置訪問權限
在Ubuntu下MySQL缺省是隻允許本地訪問的,使用workbench連接工具是連不上的;
如果你要其他機器也能夠訪問的話,需要進行配置;

3.2.1 配置遠程訪問

# 登陸mysql
sudo mysql -uroot -p
#切換數據庫
use mysql;
#查詢用戶表命令:
select User,authentication_string,Host from user;

訪問權限說明:host默認都是localhost訪問權限

 

#設置權限與密碼
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';  
#刷新cache中配置
flush privileges;  


第二個'root'爲你給新增權限用戶設置的密碼,%代表所有主機,也可以是具體的ip;

 

3.2.2 新建數據庫和用戶
這個之前就建好了,所以上邊查詢User的時候能看到:

##1 創建數據庫studentService
CREATE DATABASE studentService;
##2 創建用戶teacher(密碼admin) 並賦予其studentService數據庫的遠程連接權限
GRANT ALL PRIVILEGES ON teacher.* TO studentService@% IDENTIFIED BY "admin";

 

3.2.3 修改root賬號密碼:

mysql -uroot -p;

use mysql;

user set password=PASSWORD('新密碼') where user='root';

FLUSH PRIVILEGES;

如果你的mysql是5.7版本後的  

use mysql;

update user set authentication_string=PASSWORD('123456') where user='root';

FLUSH PRIVILEGES;

 

4.mysql服務命令
4.1 檢查服務狀態

systemctl status mysql.service
或
sudo service mysql status

顯示如下結果說明mysql服務是正常的:

weison@ubuntu19-04:~$ systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-06-29 11:10:10 CST; 15min ago
 Main PID: 8545 (mysqld)
    Tasks: 28 (limit: 4915)
   Memory: 173.4M
   CGroup: /system.slice/mysql.service
           └─8545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

6月 29 11:10:10 ubuntu19-04 systemd[1]: Starting MySQL Community Server...
6月 29 11:10:10 ubuntu19-04 systemd[1]: Started MySQL Community Server.


4.1 mysql服務啓動停止

#停止
sudo service mysql stop
#啓動
sudo service mysql start

 

5.使用workbench連接數據庫
用第3步新建的用戶teacher登陸數據庫studentService,root用戶我是咋都沒登陸進取。

5.1 打開workbench進行連接配置:

5.2 配置完成後,執行sql:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章