检查docker版本
docker --version
确保Docker启动
systemctl start docker
查看可用的 MySQL 版本
docker search mysql
拉取最新版MySQL镜像
(如果要安装指定版本将latest换成版本号即可)
docker pull mysql:latest
查看已下载的镜像
docker image ls
或者
docker images
运行容器
参数说明:
• -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
• MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
• --name 容器别名
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
设置docker 自启动
systemctl enable docker.service
查看自启动
systemctl list-unit-files | grep enable
设置 docker mysql镜像自启动
docker update c90c76bd1dae --restart=always
#2.进入mysql容器:这里的mysql-test最好使用容器的id来替换
docker exec -it mysql-test bash
#3.使用账号密码登录
mysql -uroot -p123456
#4.创建数据库:
CREATE DATABASE luckyframe CHARACTER SET utf8;
rpm -qa | grep mysql #检查是否安装过MySQL
rpm -qa | grep mariadb #检查是否存在 mariadb 数据库(内置的MySQL数据库),有则强制删除
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 #强制删除
[root@MeterSphere /home/test]$rpm -qa | grep mysql
[root@MeterSphere /home/test]$rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@MeterSphere /home/test]$rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@MeterSphere /home/test]$rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@MeterSphere /home/test]$curl -O https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25680 100 25680 0 0 21371 0 0:00:01 0:00:01 --:--:-- 21382
[root@MeterSphere /home/test]$
[root@MeterSphere /home/test]$yum localinstall mysql57-community-release-el7-11.noarch.rpm
出现以下信息表示安装成功了
[root@MeterSphere /home/test]$yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 206
mysql-tools-community/x86_64 MySQL Tools Community 94
mysql57-community/x86_64 MySQL 5.7 Community Server 624
[root@MeterSphere /home/test]$
[root@MeterSphere /home/test]$yum install mysql-community-server
安装会报错
Public key for mysql-community-client-5.7.40-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-client-5.7.40-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
安装报错,提示无公共秘钥
解决方法:
[root@MeterSphere /home/test]$rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后再次安装
[root@MeterSphere /home/test]$yum install mysql-community-server
有对应的server的信息即为成功
[root@MeterSphere /home/test]$yum list installed mysql-*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
mysql-community-client.x86_64 5.7.40-1.el7 @mysql57-communitymysql-community-common.x86_64 5.7.40-1.el7 @mysql57-communitymysql-community-libs.x86_64 5.7.40-1.el7 @mysql57-communitymysql-community-server.x86_64 5.7.40-1.el7 @mysql57-community[root@MeterSphere /home/test]$
[root@MeterSphere /home/test]$systemctl start mysqld
[root@MeterSphere /home/test]$systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-11-02 13:05:30 CST; 9s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 18745 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 18596 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 18749 (mysqld)
Tasks: 27
Memory: 320.6M
CGroup: /system.slice/mysqld.service
└─18749 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Nov 02 13:05:25 MeterSphere.teyuntong.net systemd[1]: Starting MySQL Server...
Nov 02 13:05:30 MeterSphere.teyuntong.net systemd[1]: Started MySQL Server.
[root@MeterSphere /home/test]$
[root@MeterSphere /home/test]$grep 'temporary password' /var/log/mysqld.log
2022-11-02T05:05:27.765831Z 1 [Note] A temporary password is generated for root@localhost: mhNQq>rje16d
输入以下命令后,再输入默认登录密码,就能以 root 帐号登录 mysql
[root@MeterSphere /home/test]$mysql -uroot -p
在创建数据库或数据表之前,要先用一下命令修改 root 用户的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
grant all privileges on . to ‘root’ @‘%’ identified by ‘Mysql@123’;
第一个*是数据库,可以改成允许访问的数据库名称
第二个 是数据库的表名称,代表允许访问任意的表
root代表远程登录使用的用户名,可以自定义
%代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了
password代表远程登录时使用的密码,可以自定义
让权限立即生效:flush privileges
grant all privileges on *.* to 'root' @'%' identified by 'Mysql@123456' with grant option;
mysql> grant all privileges on *.* to 'root' @'%' identified by 'Mysql@123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
[root@MeterSphere /home/test]$systemctl enable mysqld
[root@MeterSphere /home/test]$cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@MeterSphere /home/test]$
重启
systemctl restart mysqld
强制删除已安装MySQL
rpm -qa | grep mysql
find / -name mysql
rm -f /etc/my.cnf