阿里云轻量应用服务器环境搭建-Docker安装mysql

    技术2023-05-16  90

    阿里云轻量应用服务器环境搭建-Docker安装mysql

    参考链接:https://www.runoob.com/docker/docker-install-mysql.html

    查看可用的 MySQL 版本 docker search mysql

    拉取 MySQL 镜像 docker pull mysql:latest

    查看本地镜像

    运行容器

    docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql # 注意,这里的mysql-test建议改为mysql,方便以后使用。

    参数说明:

    -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。

    MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。

    查看是否安装成功: docker ps

    使用mysql -u root -p连接mysql失败

    使用下面命令排查:可以看到,mysql确实安装成功,并且也启动了。 解决:要先进入容器里再连接 docker exec -it mysql-test /bin/bash #mysql-test是上面新建容器时起的名字 mysql -u root -p

    原因:

    最新官方MySQL(5.7.19)的docker镜像在创建时映射的配置文件目录有所不同,在此记录并分享给大家:

    官方原文:

    The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

    大概意思是说:

    MySQL(5.7.19)的默认配置文件是 /etc/mysql/my.cnf 文件。如果想要自定义配置,建议向 /etc/mysql/conf.d 目录中创建 .cnf 文件。新建的文件可以任意起名,只要保证后缀名是 cnf 即可。新建的文件中的配置项可以覆盖 /etc/mysql/my.cnf 中的配置项。

    MySQL建数据库,建表常用命令

    建立数据库 create database EasyMemo; # 创建数据库 use EasyMemo; # 使用创建的数据库 show tables; # 查看有哪些表

    建表 create table user ( user_account varchar(11) not null primary key );

    create table memo ( memo_id int(10) auto_increment primary key, memo_content varchar(100) not null, memo_account varchar(50) not null, memo_password varchar(50) not null, memo_note varchar(300) null, memo_date date not null, user_account varchar(11) not null, constraint memo_user_user_account_fk foreign key (user_account) references user (user_account) on update cascade on delete cascade );

    本地的Navicat连接docker中部署的mysql

    本地的Navicat连接docker中部署的mysql,首先执行允许远程登录 ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

    在阿里云控制台添加防火墙规则,打开3306端口(重要!!!在阿里云服务器中安装什么东西的时候,开了端口映射就要记得在控制台中添加防火墙规则,打开对应的端口。)(之前就是因为没打开端口,报错,找原因找的人有一万句mmp 想说,wc )

    再使用Navicat连接,就可以成功!

    创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!

    Processed: 0.015, SQL: 9