一、Centos 安装MySQL
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
1. 安装MySQL5.7
- 下载并安装MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm yum -y install mysql57-community-release-el7-10.noarch.rpm yum -y install mysql-community-server yum -y remove mysql57-community-release-el7-10.noarch systemctl start mysqld.service systemctl status mysqld.service
- 查看MySQL默认的root密码
grep "password" /var/log/mysqld.log
- 修改root密码
mysql -uroot -p ALTER USER 'root'@'%' IDENTIFIED BY 'new password'; flush privileges;
- 修改配置文件
// do something...
2. 安装MySQL8.0
- 下载并安装MySQL官方的 Yum Repository
wget -i -c https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm yum -y install mysql80-community-release-el7-1.noarch.rpm yum -y install mysql-community-server yum -y remove mysql80-community-release-el7-1.noarch systemctl start mysqld.service systemctl status mysqld.service
- 查看MySQL默认的root密码
grep "password" /var/log/mysqld.log
- 修改root密码
mysql -uroot -p ALTER USER 'root'@'%' IDENTIFIED BY 'new password' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'new password'; flush privileges;
- 修改配置文件
// do something...
二、Docker 安装MySQL
1. 安装5.7
version: '3'
services:
# ref:https://hub.docker.com/_/mysql
# ref:https://docs.docker.com/samples/library/mysql/#-via-docker-stack-deploy-or-docker-compose
mysql:
image: mysql:5.7.22
container_name: mysql
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
# data 用来存放了数据库表文件,init存放初始化的脚本
volumes:
- ./mysql/data/:/var/lib/mysql/
- ./mysql/conf/my.cnf:/etc/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
restart: always
ports:
- "3306:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root
2. 安装8.0
version: '3'
services:
# ref:https://hub.docker.com/_/mysql
# ref:https://docs.docker.com/samples/library/mysql/#-via-docker-stack-deploy-or-docker-compose
mysql:
image: mysql:8.0
container_name: mysql
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
# data 用来存放了数据库表文件,init存放初始化的脚本
volumes:
- ./mysql/data/:/var/lib/mysql/
- ./mysql/conf/my.cnf:/etc/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
restart: always
ports:
- "3306:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: dev
MYSQL_PASSWORD: dev123
3. docker-compose安装
#写好docker-compose.yml文件后使用命令启动容器
docker-compose up -d
docker-compose ps
三、MySQL卸载
四、注意事项
1. com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的,com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的;MySQL8需要使用com.mysql.cj.jdbc.Driver驱动。
mysql8.0驱动:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
<scope>runtime</scope>
</dependency>
2. MQL5.x 和MySQL8.x使用的注意事项
jdbc:mysql://localhost:3306/database?useUnicode=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
- 驱动:选择com.mysql.cj.jdbc.Driver而不是com.mysql.jdbc.Driver。
- 时区:主动指定时区
- serverTimezone=UTC (比中国晚8小时)
- serverTimezone=GMT%2B8 (GMT+8 北京时间)
- serverTimezone=Shanghai (上海时间)
- serverTimezone=Hongkong (香港时间)
- SSL:如果未明确设置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接;如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。
3. 连接mysql 8错误:authentication plugin ‘caching_sha2_password‘
该异常原因是:在mysql8之前的版本使用的密码加密规则是mysql_native_password,但是在mysql8则是caching_sha2_password。
- 解决方案
docker exec -ti mysql /bin/bash mysql -u root -p root123; ALTER USER 'root'@'%' IDENTIFIED BY 'your password' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your password'; FLUSH PRIVILEGES;
原文地址:https://www.cnblogs.com/kancy/p/install_mysql.html
时间: 2024-11-09 05:17:54