docker 安装mysql8

904 阅读1分钟
#拉取最新mysql镜像
$ docker pull mysql

#挂载本机配置
$ mkdir -p /data/www-data/docker/mysql
$ cd /data/www-data/docker/mysql
$ mkdir conf logs data
$ cd conf
$ touch my.cnf 
my.cnf文件添加内容
[mysqld]
user=mysql
character-set-server=utf8
secure_file_priv=/var/lib/mysql
expire_logs_days=7
skip-grant-tables #跳过密码验证

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

#启动容器
$ docker run -p 3306:3306 --name docker-mysql --privileged=true \
-v /data/www-data/docker/mysql/conf:/etc/mysql \
-v /data/www-data/docker/mysql/logs:/var/log/mysql \
-v /data/www-data/docker/mysql/data:/var/lib/mysql \
-d mysql

#进入容器
$ docker exec -it '容器ID' /bin/bash

#进入mysql
$ mysql
#显示数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

#选择数据库
mysql> use mysql;

#显示数据库中的表
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| component                 |
| 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> ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxx';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

#执行flush privileges后再修改
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxx';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#远程连接(msyql8分配权限不能带密码)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
原因是 mysql 数据库中 user 表中的特定用户(root) 的 host 的属性值为localhost

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)