1.拉取鏡像
docker pull mysql:latest
2.掛載運行
--name:容器命名為mysql8-p:將容器的3306端口映射到宿主機的13306端口-d: 后台運行--restart=always: docker重啟后,容器也隨之啟動-v:目錄掛載-e MYSQL_ROOT_PASSWORD:初始化root賬號的密碼為root
docker run --name mysql8 -p 13306:3306 -e MYSQL_ROOT_PASSWORD=root --restart=always -v /orisdom/mysql/conf.d:/etc/mysql/conf.d -v /orisdom/mysql/my.cnf:/etc/mysql/my.cnf -v /orisdom/mysql/data:/var/lib/mysql -d mysql:latest
3.在容器中登陸MySql
mysql -u root - p
默認密碼: root
4.檢查訪問權限
use mysql select user,host from user;
若權限不存在則設置:update user set host='%' where user='root';
5.設置密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密碼'; alter user 'root'@'%' identified with mysql_native_password BY '新密碼'; flush privileges;
6.修改字符編碼和MySql5.7以后分組問題
vim /etc/mysql/my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
default-character-set=utf8
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
default-time_zone = +8:00
secure-file-priv= NULL
# 允許最大連接數
max_connections=1000
# ================= ↓↓↓ mysql主從同步配置start ↓↓↓ =================
# 同一局域網內注意要唯一
server-id=223
# 開啟二進制日志功能,以備slave作為其它slave的master時使用
log-bin=mysql-slave-bin
# Slave_SQL_Running 為NO 日志中報錯1032 ,只需要在slave中配置該項
# slave-skip-errors = 1032
# ================= ↑↑↑ mysql主從同步配置end ↑↑↑ =================
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
[mysql]
default-character-set=utf8
[mysqld_safe]
log-error =/orisdom/mysql/error
# Custom config should go here
!includedir /etc/mysql/conf.d/
建議從宿主機拷貝:docker cp ./my.cnf 容器ID:/etc/mysql/my.cnf
my.cnf鏈接:https://pan.baidu.com/s/1-huR9S9dyk2hKXZsd_fkSA 提取碼:xpfr
重啟登錄驗證:show variables like 'character%';
7.調整時區,保證數據庫時間時間與系統一致
查看時區:show VARIABLES like '%time_zone%';
查看時間:select now();
set time_zone = '+8:00'; set global time_zone = '+8:00'; flush privileges;
查詢時間驗證:select now();
8.Navicat連接測試

9.pom依賴版本
驅動版本:<mysql.version>8.0.11</mysql.version>
連接池版本:<druid.version>1.1.10</druid.version>
連接屬性設置:url:jdbc:mysql://192.168.10.226:3307/sewage_ls?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true
10.項目啟動測試

