$ 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> 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
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)
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)