在linux中安装mysql
docker安装
#使用docker 下载mysql镜像
docker pull mysql:5.7
#启动mysql:5.7并设置密码
docker run --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
docker restart 容器id # 重启容器
#进入容器
docker exec -it mysql5.7(容器名) /bin/bash
#登录mysql
mysql -u root -p
#查看时区
show variables like "%time_zone%";
#修改mysql全局时区为北京时间,即我们所在的东8区
set global time_zone = '+8:00';
#修改当前会话时区
set time_zone = '+8:00';
#立即生效
flush privileges;
[root@VM-12-8-centos oauth]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05004f246a11 mysql:5.7 "docker-entrypoint.s…" 3 months ago Up 4 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7
d0f4a0e5ce16 tomcat:9.0 "catalina.sh run" 3 months ago Up 3 weeks 8080/tcp amazing_shannon
[root@VM-12-8-centos oauth]# docker exec -it mysql5.7 /bin/bash
root@05004f246a11:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30113
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like "%time_zone%";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | UTC |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
mysql> set global time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)
mysql> set time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
修改mysql的密码
#1、查看容器服务
docker ps
#2、进入mysql容器
docker exec -it mysql(容器名) /bin/bash
#3、登录MySQL
mysql -u root -p
#5、修改MySQL密码
SET PASSWORD FOR 'root' = PASSWORD('密码');
#6、修改本地MySQl密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('密码');
#7、退出mysql
quit
#8、ctrl+p+q退出mysql容器
docker-compose安装启动sql
version: '3'
services:
mysql:
image: mysql:5.7.28
restart: always
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: BKS@123456
TZ: Asia/Shanghai
ports:
- 3307:3306
volumes:
- /usr/bks/mysql/data:/var/lib/mysql
- /usr/bks/mysql/config/my.cnf:/etc/mysql/my.cnf
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
注意:my.cnf文件不是让服务创建(会创建成为一个文件夹),只能自己创建