先查询mysql源
docker search mysql
也可以去官网查看镜像tag,选择自己需要的版本,否则会下载最新版本:https://hub.docker.com/_/mysql/
然后报错了!!!
root@deepin-pc:/etc/apt# docker pull mysql:8.0.11
error response from daemon: get https://registry-1.docker.io/v2/library/mysql/manifests/8.0.11: get https://auth.docker.io/token?scope=repository%3alibrary%2fmysql%3apull&service=registry.docker.io: net/http: tls handshake timeout
换成国内的镜像源
echo "docker_opts=\"\$docker_opts --registry-mirror=http://f2d6cb40.m.daocloud.io\"" | sudo tee -a /etc/default/docker
重启docker
sudo service docker restart
再试一下
root@deepin-pc:/etc/docker# docker pull mysql:8.0.11
8.0.11: pulling from library/mysql
be8881be8156: pull complete
c3995dabd1d7: pull complete
9931fdda3586: pull complete
bb1b6b6eff6a: pull complete
a65f125fa718: pull complete
2d9f8dd09be2: pull complete
37b912cb2afe: pull complete
90a9e6fd6a27: pull complete
959ebd3ef120: pull complete
5eda665eddc4: pull complete
d9007173a367: pull complete
239f4d989075: pull complete
digest: sha256:ffa442557c7a350939d9cd531f77d6cbb98e868aeb4a328289e0e5469101c20e
status: downloaded newer image for mysql:8.0.11
docker.io/library/mysql:8.0.11
验证一下
root@deepin-pc:/etc/docker# docker images
repository tag image id created size
mysql 8.0.11 5dbe5b6313e1 21 months ago 445mb
为docker创建一个专门放mysql 的文件夹
root@deepin-pc:/etc/docker# cd /opt/
root@deepin-pc:/opt# mkdir mysql_docker
root@deepin-pc:/opt# cd mysql_docker/
root@deepin-pc:/opt/mysql_docker# echo $pwd
/opt/mysql_docker
启动mysql容器
docker run --name mysqlserver -v $pwd/conf:/etc/mysql/conf.d -v $pwd/logs:/logs -v $pwd/data:/var/lib/mysql -e mysql_root_password=123456 -d -i -p 3306:3306 mysql:8.0.11
查看完整容器id
cd /var/lib/docker/containers/
查看启动的容器
1
2
3
|
root@deepin-pc: /opt/mysql_docker # docker ps
container id image command created status ports names
1f9aa5b79b6a mysql:latest "docker-entrypoint.s…" about a minute ago up 59 seconds 0.0.0.0:3306->3306 /tcp , 33060 /tcp mysql
|
进入mysql(可以用容器id替换mysql)
docker exec -it mysql bash
连接mysql
mysql -u root -p 123456
授予远程访问权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
mysql> use mysql;
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a
database changed
mysql> select host, user from user ;
+ -----------+------------------+
| host | user |
+ -----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+ -----------+------------------+
5 rows in set (0.00 sec)
mysql> alter user 'root' @ '%' identified with mysql_native_password by '123456' ;
query ok, 0 rows affected (0.02 sec)
mysql> flush privileges ;
query ok, 0 rows affected (0.01 sec)
|
查看docker日志:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
root@deepin-pc: /opt/mysql_docker # docker ps -a
container id image command created status ports names
1f9aa5b79b6a mysql:latest "docker-entrypoint.s…" 6 minutes ago up 6 minutes 0.0.0.0:3306->3306 /tcp , 33060 /tcp mysql
root@deepin-pc: /opt/mysql_docker # docker logs -f --tail 10 1f9aa5b79b6a
2020-05-11 14:14:30+00:00 [note] [entrypoint]: mysql init process done . ready for start up.
2020-05-11t14:14:30.702850z 0 [warning] [my-011070] [server] 'disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. consider not using this option as it' is deprecated and will be removed in a future release.
2020-05-11t14:14:30.702952z 0 [system] [my-010116] [server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 1
2020-05-11t14:14:30.712787z 1 [system] [my-013576] [innodb] innodb initialization has started.
2020-05-11t14:14:31.064937z 1 [system] [my-013577] [innodb] innodb initialization has ended.
2020-05-11t14:14:31.191792z 0 [system] [my-011323] [server] x plugin ready for connections. socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2020-05-11t14:14:31.284386z 0 [warning] [my-010068] [server] ca certificate ca.pem is self signed.
2020-05-11t14:14:31.292565z 0 [warning] [my-011810] [server] insecure configuration for --pid- file : location '/var/run/mysqld' in the path is accessible to all os users . consider choosing a different directory.
2020-05-11t14:14:31.312549z 0 [system] [my-010931] [server] /usr/sbin/mysqld : ready for connections. version: '8.0.20' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mysql community server - gpl.
|
一些常用操作
先查看容器的id
docker ps -a
查到id
以下id为容器id
关闭mysql
docker stop id
重启
docker restart id
启动
docker start id
**关闭docker **
systemctl stop docker
重启docker
systemctl restart docker
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/syrgdm/p/12872752.html