cd /opt
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum localinstall mysql57-community-release-el7-11.noarch.rpm
yum -y install mysql-community-server
systemctl start mysqld
systemctl status mysqld
systemctl enable mysqld
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'new password';
flush privileges;
此时,即可以通过root和设置的新密码远程连接mysql数据库了
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11731 [OK]
mariadb MariaDB Server is a high performing open sou… 4472 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 875 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 381 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 89
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 53
prom/mysqld-exporter 43 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 23
centos/mysql-56-centos7 MySQL 5.6 SQL database server 21
arey/mysql-client Run a MySQL client from a docker container 19 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 16 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 8 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 2
jelastic/mysql An image of the MySQL database server mainta… 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a10c77af2613: Pull complete
b76a7eb51ffd: Pull complete
258223f927e4: Pull complete
2d2c75386df9: Pull complete
63e92e4046c9: Pull complete
f5845c731544: Pull complete
bd0401123a9b: Pull complete
2724b2da64fd: Pull complete
d10a7e9e325c: Pull complete
1c5fd9c3683d: Pull complete
2e35f83a12e9: Pull complete
Digest: sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
docker run -d -p 3306:3306 --privileged=true -v /docker/mysql/etc/my.cnf:/etc/my.cnf -v /docker/mysql/etc/mysql:/etc/mysql -v /docker/mysql/var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxxxxxx --name mysql mysql:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci
参数说明:
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4afca70d4674 mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
373b8eaed36f jenkins/jenkins:lts "/sbin/tini -- /usr/…" 8 days ago Up 3 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp, 50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp myjenkins
1906e95db7ab nginx "/docker-entrypoint.…" 7 weeks ago Up 7 days 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker exec -it 4afca70d4674 bash
root@4afca70d4674:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
root@4afca70d4674:/# exit
exit
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
进入docker,登录mysql数据库后,执行如下命令即可(xxxxxx为数据库密码)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxxxx';
flush privileges;
如
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4afca70d4674 mysql:5.7 "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
373b8eaed36f jenkins/jenkins:lts "/sbin/tini -- /usr/…" 8 days ago Up 3 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp, 50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp myjenkins
1906e95db7ab nginx "/docker-entrypoint.…" 7 weeks ago Up 7 days 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker exec -it 4afca70d4674 bash
root@4afca70d4674:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
此时即可使用root用户远程连接了
如下,暴露一个用户名为mysql的用户
GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
flush privileges;
如:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
此时即可使用mysql用户远程连接了
SET NAMES 'utf8';
编写yaml如下
apiVersion: v1
kind: Namespace
metadata:
name: devops # Namespace 的名称
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: devops-mysql # deployment控制器名称
namespace: devops
spec:
replicas: 1
revisionHistoryLimit: 5
strategy:
type: RollingUpdate
selector:
matchLabels:
app: devops-mysql
template:
metadata:
labels:
app: devops-mysql
spec:
volumes:
- name: devops-mysql
nfs:
server: xx.xx.xx.xx # 修改为挂载存储的服务器ip
path: /root/data/nfs/mysql/devops # 修改为存储服务器的存储挂载路径
containers:
- name: devops-mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: xxxxxxxx # 设置MySQL数据库登录密码
imagePullPolicy: Always
ports:
- containerPort: 3306
volumeMounts:
- name: devops-mysql
mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: devops-mysql # 数据库服务的名称
namespace: devops
spec:
ports:
- port: 3306
protocol: TCP
targetPort: 3306
nodePort: 30001 # 对外访问的端口
selector:
app: devops-mysql
type: NodePort
sessionAffinity: ClientIP
kubectl apply -f mysql.yaml
使用如下命令查看
kubectl get pod -n devops | grep mysql
如:
[root@master ~]# kubectl get pod -n devops | grep mysql
devops-mysql-59b68c47d4-ttbng 1/1 Running 0 23h
[root@master ~]#
如下;
[root@master ~]# kubectl exec -it devops-mysql-59b68c47d4-ttbng bash -n devops
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@devops-mysql-59b68c47d4-ttbng:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql>
这里需要说明一下,安装windows版的数据库一般是在学习数据库的时候,或者个人没有云主机,或者还未自己搭建过linux虚拟机等等情况下,在实际企业中,基本都是在linux系统上安装mysql数据库,所以如果自己有云主机或者自己有搭建linux虚拟机,建议直接在linux虚拟机上安装部署mysql使用,当然使用docker或者Kubernetes安装mysql都是一样的,安装步骤也都在上面列出了,因此,如果没有linux虚拟机或云主机,也可以参照如下步骤在windows安装一个数据库,然后对mysql数据库进行系统的学习
(2)Oracle公司把收费的版本挂载非常醒目的位置,社区免费版则放在不那么显眼的位置,点击如下处的链接
(4)既然在window上安装,就去下载msi格式的安装文件,点击如下位置的链接
(5)这里选择数据大的一个,数据小的那个其实并没有下载,而是在安装的时候再下载,这里建议直接下载下来,选择文件大的那个版本,当前最新的版本已经是8.0.29了
(7)如果想下载老的版本,比如下载5.7系列的,先反回到上述(5)的页面,点击【Articles】标签
(8)然后选择对应的版本,比如选择5.7系列的最新的版本,然后同样选择文件大的版本,点击【下载】,然后就开始下载了
(1)找到下载安装文件,【右键】-【安装】,或者直接双击也可以,为了看看安装的过程,建议选择【自定义安装】
(2)这里仅安装mysql的server就OK了,application下面的是一些客户端工具,如果有喜欢用的也可以安装,这里就安装了,选中server,点击向右的按钮,就选过来了
(3)此时先选中【MySQL server】,然后下面就会出现【Advanced Options】,点击
(4)第一个是软件安装的位置,可以修改自己的其他目录,第二个是数据库中存放数据的位置,这里就不修改这个了,如下
(5)这时会弹出一个告警,直接点【OK】,然后点击【next】
(6)此时产品处已经有这个MySQL Server了,路径也是上面设置后的了,然后点【next】
(9)如下,已经安装完成了,下面就开始配置数据库了,点击【next】
(11)首先选择配置类型,这里提供了三种模式,第一种是开发者模式,占用内存比较少,第二种是专门数据库服务,占用中等量的内存,第三种是将此机器只用来部署数据库,即占用所有可用的内存,因此,这里学习用就配置使用开发者模式即可
(12)数据库默认使用3306端口,这里默认不修改,点击【next】
(13)可以看到8.0版本提供了一种更加安全的加密方式,当然也支持5.x版本的加密算法的方式。这里选择8.0推荐的更加安全的方式
(14)设置root用户的密码,当然这里可以添加用户,这里暂时先不添加了,只设置root用户的密码
(15)这里是服务的名称,以及是否随电脑一起启动和是否以管理员的角色运行数据库服务,这里都采用默认配置,即如下
(20)安装完成后,在磁盘打开设置的安装路径下,如下就是mysql数据库安装后的安装目录中的文件
(22)然后设置环境变量,在【计算机】-右键-【属性】-【高级系统设置】-【高级】-【环境变量】-【系统变量】-找到Path双击打开-【新建】,然后将mysql的安装目录下的bin目录的路径拷贝过来,比如这里是:D:\ProgrameFile\MySQLServer8.0\bin
(23)然后重新打开一个cmd窗口,执行 mysql -uroot -p ,然后提示输入密码,,输入密码后就登录进mysql数据库了
至此,MySQL8.0的数据库就已经安装配置完成了
这里说明一下,一个电脑上是完全可以安装多个数据库的,这里就是在8.0版本安装完成之后,直接安装5.7版本的,即在windows上同时安装5.7版本和8.0版本的数据库
(1)双击打开5.7版本的数据库的安装文件,然后点击【ADD】
(2)然后选择64位对应的5.7版本的mysql,然后点击向右的箭头
(3)选中右侧的【Mysql Server】,然后下面就会出现【Advanced Option】,点击
(4)同样修改一下mysql软件安装位置,数据存储位置暂时不做修改
(10)类型选择开发者模式,这里注意端口需要改一下,因为8.0版本的数据库已经把3306端口占了,这里可以改为比如13306
(17)在安装8.0版本的时候,到这个位置应该配置环境变量了,那么在这里就不需要配置了,配置环境变量的目的是为了在cmd窗口中使用mysql命令即mysql命令行的客户端工具,因为8.0已经配置了,也就是说目前cmd中mysql的命令仍然是有效的,因此可以直接使用mysql去连接5.7版本的数据库,只需要在连接命令中设置端口号即可,如下打开两个cmd窗口,分别通过 -P指定不同的端口来登录不同的数据库
至此在windows上就完成了同时安装8.0版本和4.7版本的数据库了