阿里雲輕量應用服務器環境搭建-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 themysql
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連接,就可以成功!
創作不易,喜歡的話加個關注點個贊,謝謝謝謝謝謝!