1、拉取指定版本MySQL
docker pull mysql/mysql-server:5.7
2、启动容器,并映射本地路径
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -v /mnt/tools/mysql/data:/mnt --name mysql mysql/mysql-server:5.7
3、检查运行情况
docker ps
4、传输数据库到容器
docker cp /mnt/sys_user.sql 30d51d9cec2b:/mnt
5、进入数据库容器
docker exec -it mysql bash
6、登录数据库
mysql -uroot -p
7、执行文件
source /mnt/sys_user.sql
8、遇到问题
8.1 时区不对
可以在启动项里面加入-e TZ="Asia/Shanghai"
8.2 大小写敏感
可以在容器中/etc/mysql/mysql.conf.d/mysqld.cnf中添加 lower_case_table_names=1
8.3 中文乱码
可以在容器中/etc/mysql/mysql.conf.d/mysqld.cnf中添加
[mysqld]
character-set-server=utf8
character_set_server=utf8
init_connect=’SET NAMES utf8’
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
8.4 控制台不能输入中文
可以在启动项中添加 -e LANG=C.UTF-8
8.5 /etc/mysql/mysql.conf.d/mysqld.cnf完整内容如下
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
symbolic-links=0
character-set-server=utf8
lower_case_table_names=1
character_set_server=utf8
init_connect=’SET NAMES utf8’
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
9、 mysql8有大坑
启动语句是
docker run -e LANG=C.UTF-8 -e TZ="Asia/Shanghai" -p 3306:3306 --ulimit nofile=65536:65536 --name mysql \ -v /home/tools/mysql/logs:/var/log/mysql \ -v /home/tools/mysql/data2:/var/lib/mysql \ -v /home/tools/mysql/my.cnf:/etc/mysql/my.cnf \ -e MYSQL_ROOT_PASSWORD=@wecredo2022 \ -d 667ee8fb158e --lower-case-table-names=1
my.cnf是
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL lower_case_table_names=1 default-time_zone='+8:00' max_connections=2000 join_buffer_size = 512M tmp_table_size = 512M max_allowed_packet = 100M interactive_timeout = 1200 read_buffer_size = 64M read_rnd_buffer_size = 8M sort_buffer_size = 8M innodb_buffer_pool_size = 8G innodb_buffer_pool_instances = 1 innodb_log_file_size = 1G innodb_log_buffer_size = 256M table_open_cache=2048 innodb_buffer_pool_dump_pct = 40 innodb_page_cleaners = 4 innodb_undo_log_truncate = 1 innodb_max_undo_log_size = 1G sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' character-set-server=utf8 init_connect=’SET NAMES utf8’ [mysql] default-character-set = utf8 [mysql.server] default-character-set = utf8 [mysqld_safe] default-character-set = utf8 [client] default-character-set = utf8 !includedir /etc/mysql/conf.d/
mysql5和mysql8的配置文件路径不一样,内容也不一样,坑!!!