【docker】 centos7 下 使用docker 安装mysql

1 获取 mysql 镜像

docker pull  mysql:5.7

2 创建mysql的镜像,并运行

docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=php@666 --name codeforphp_mysql mysql:5.7

参数说明 
-d 让容器在后台运行 
-p 添加主机到容器的端口映射 
-e 设置环境变量,这里是设置mysql的root用户的初始密码,这个必须设置 
–name 容器的名字,随便取,但是必须唯一

ps: 其实我们可以仅仅使用docker run命令就行了。docker run会先去pull,然后再create。个人习惯先把镜像pull下来,在run的时候会很快。

3 进入mysql 终端

docker exec -it   codeforphp_mysql    bash

参数说明 
-t 在容器里生产一个伪终端 
-i 对容器内的标准输入 (STDIN) 进行交互

输入命令。进入

root@f489cae3f0f4:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> 

进入之后,要对用户进行授权,以便使用远程连接

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'php@666';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'php@666';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'php@666';
FLUSH PRIVILEGES;

此时可以使用客户端连接一下看看。注意,客户端连接端口为 3307 

猜你喜欢

转载自www.cnblogs.com/richerdyoung/p/9198397.html