创建目录
mkdir -p /mydata/mysql8/log /mydata/mysql8/data /mydata/mysql8/conf
新增my.cnf配置
/mydata/mysql8/conf 目录下执行 vim my.cnf
#
# 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
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
character-set-server=utf8mb4
default-storage-engine=INNODB
#取消 group 严格模式
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#忽略大小写
lower_case_table_names=1
#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
拉取镜像
docker run -p 3306:3306 --name mysql8 \
-v /mydata/mysql8/log:/var/log/mysql \
-v /mydata/mysql8/data:/var/lib/mysql \
-v /mydata/mysql8/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
--privileged=true \
-d mysql:8.0.24 --lower-case-table-names=1
mysql:8.0.24,冒号后面是版本号,去除 :8.0.24,将拉去最新版本镜像
验证
# 查看运行中的容器
docker ps
# 进入容器
docker exec -it mysql8 bash
# 登录mysql,设置远程登录
mysql -uroot -proot
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
# 更改密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;