docker pull mysql:8.0
docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0
docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -v /mqq/mysql8/datadir:/var/lib/mysql -d mysql:8.0
这里,-v /data/mysql8/datadir:/var/lib/mysql 将主机上的/data/mysql8/datadir目录挂载到容器内的/var/lib/mysql,用于持久化数据。
请确保替换命令中的路径和密码为您自己的实际路径和密码。
docker run -p 3307:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.20
docker ps
mkdir -p /mqq/mysql8.0.20/
docker cp mysql:/etc/mysql /mqq/mysql8.0.20/
docker rm -f mysql
cd /mqq/mysql8.0.20/mysql/conf.d
cat > my.cnf << EOF
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[mysql]
EOF
docker run \
-p 3336:3306 \
--name mysql8 \
--privileged=true \
--restart unless-stopped \
-v /mqq/mysql8.0.20/mysql:/etc/mysql \
-v /mqq/mysql8.0.20/logs:/logs \
-v /mqq/mysql8.0.20/data:/var/lib/mysql \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0.20
-p 端口映射 【后续链接数据库的时候用3336链接不用3306】
–name mysql8 名称是mysql8
–privileged=true 挂载文件权限设置
–restart unless-stopped 设置 开机后自动重启容器
-v /mqq/mysql8.0.20/mysql:/etc/mysql \ 挂载配置文件【路径是上面创建的挂载路径】
-v /mqq/mysql8.0.20/logs:/logs \ 挂载日志【路径是上面创建的挂载路径】
-v /mqq/mysql8.0.20/data:/var/lib/mysql \ 挂载数据文件 持久化到主机【路径是上面创建的挂载路径】
-v /etc/localtime:/etc/localtime 容器时间与宿主机同步
-e MYSQL_ROOT_PASSWORD=123456 设置密码
-d mysql:8.0.20 后台启动,mysql
docker ps
docker exec -it mysql8 bash #进入mysql容器
mysql -u root -p123456
grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION; #为root分配权限,以便可以远程连接
use mysql
update user set host='%' where user='root';
#由于Mysql5.6以上的版本修改了Password算法,这里需要更新密码算法,便于使用Navicat连接
grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
ALTER user 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
-- 创建新数据库 (替换your_database_name为你的数据库名)
CREATE DATABASE school;
-- 选择数据库
USE school;
-- 创建一个名为 class的表,包含三列:id, name, age
CREATE TABLE Class1Grade1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL
);
-- 插入数据
INSERT INTO Class1Grade1 (name, age) VALUES ('Alice', 6);
INSERT INTO Class1Grade1 (name, age) VALUES ('Bob', 6);
INSERT INTO Class1Grade1 (name, age) VALUES ('Charlie', 7);
INSERT INTO Class1Grade1 (name, age) VALUES ('马强', 7);
INSERT INTO Class1Grade1 (name, age) VALUES ("刘洪", 7);