日期:2019-11-02  浏览次数:1455 次

查询 : docker search mysql
下载镜像 : docker pull mysql:latest

  • 可指定MYSQL版本: docker pull mysql:8.0
  • 可指定MYSQL版本: docker pull mysql:8.1

安装并运行 : docker run -p 3306:3306 --name mymysql -v $PWD/mysql/logs:/logs -v $PWD/mysql/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest sleep infinity
进入容器 : docker exec -it mysql bash
启动mysql : 进入 : cd /etc/init.d  启动 : ./mysql start
进入mysql : mysql -uroot -p
显示所有数据库 : show databases
进入数据库 : use mysql
显示所有表 : show tables
建库 : CREATE DATABASE test;
授权 : 在mysql中执行: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;  

实操代码:

配置MYSQL代码:

docker pull mysql

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

映射本地电脑3306到docker mysql 3306端口中。 

连接数据库

root@ff723ddf4b2b:/# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@ff723ddf4b2b:/#  mysql -uroot -p123456
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


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

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |se selected
+---------------------------+
| columns_priv              | completion of table and column names
| component                 | to get a quicker startup with -A
| db                        |
| default_roles             |
| engine_cost               |
| func                      |
| general_log               |
| global_grants             |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| password_history          |
| plugin                    |
| procs_priv                |
| proxies_priv              |
| role_edges                |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
33 rows in set (0.00 sec)

mysql>