天天看點

在docker中安裝mysql詳細攻略一、安裝mysql

一、安裝mysql

  1. 查詢mysql

    docker search mysql

  2. 下載下傳需要的版本

    docker pull mysql:tag

    tag代表版本号,沒有代表是lastest的
  3. 根據鏡像啟動容器

    docker run -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

  4. 查詢mysql運作狀态

    docker ps

    如果正常啟動,就能查到如下結果
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b4b69c5e900f        mysql               "docker-entrypoint..."   34 minutes ago      Up 30 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql01
           
  1. 連接配接資料庫

    docker exec -it mysql01 bash

    那個***mysql01***是設定的容器名,執行完後進入到如下的界面
[[email protected] ~]# docker exec -it mysql01 bash
[email protected]:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.17 MySQL Community Server - GPL
           
  1. 檢視mysql是否正常啟動

    show databases

mysql> show databases;  
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

           

出現上面的那個說明資料庫已經正常啟動

7. 在虛拟機外使用Navicate連接配接資料庫可能會出現如下的異常

7.1. 檢視使用者資訊
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | root             | mysql_native_password | *E6CC90B878B948C35E92B003C792C46C58C4AF40                              |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
*z3uANJFSvbTEwhOS.Xi/Wbu7zgLBnxcUq4mjjz6uWSC |password | $A$005$xNK;^eO n"YNh.#LV
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)

           

備注:host為 % 表示不限制ip localhost表示本機使用 plugin非mysql_native_password 則需要修改密碼

7.2. 修改權限和使用者密碼

修改權限的指令:

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'newpassword'

修改後再次查詢:

select host,user,plugin,authentication_string from mysql.user;

+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | root             | mysql_native_password | *E6CC90B878B948C35E92B003C792C46C58C4AF40                              |
| %         | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
*z3uANJFSvbTEwhOS.Xi/Wbu7zgLBnxcUq4mjjz6uWSC |password | $A$005$xNK;^eO n"YNh.#LV
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
           
7.3. 如果還是沒有連接配接上,很有可能是防火牆的問題,在防火牆上添加3306端口