//做mysql镜像并使用容器模式的网络
[root@localhost ~]# docker run -it –name mysql01 –network container:nginx05 centos:latest /bin/bash
[root@3367881fd446 /]# #启动本地镜像centos 在里面安装MySQL –network container:nginx05 (以nginx05容器ID为共享网络)
//重新打开一个终端查看并上传mysql包
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
466e7f22d1f3 centos:latest “/bin/bash” 11 seconds ago Up 10 seconds mysql01 # mysql容器正运行
c193fcb41eae 1314444/source_nginx:nginx-1.21.4 “/usr/local/nginx/sb…” 9 hours ago Up 34 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx05
# mysql容器正运行
在这里插入代码片[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# docker cp mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mysql01:/usr/src
//MySQL容器下查看
[root@c193fcb41eae /]# ls /usr/src/
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
////创建用户和组
[root@c193fcb41eae /]# useradd -r -M -s /sbin/nologin mysql
[root@c193fcb41eae /]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)
//清理环境以及安装依赖包
[root@c193fcb41eae /]# yum clean all
[root@c193fcb41eae /]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs which numactl libaio
//解压
[root@c193fcb41eae /]# cd /usr/src/
[root@c193fcb41eae src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@c193fcb41eae src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.34-linux-glibc2.12-x86_64 sbin share src
//创建软连接
[root@c193fcb41eae src]# cd /usr/local/
[root@c193fcb41eae local]# ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@c193fcb41eae local]# ls -l
total 0
drwxr-xr-x 2 root root 6 Nov 3 2020 bin
drwxr-xr-x 2 root root 6 Nov 3 2020 etc
drwxr-xr-x 2 root root 6 Nov 3 2020 games
drwxr-xr-x 2 root root 6 Nov 3 2020 include
drwxr-xr-x 2 root root 6 Nov 3 2020 lib
drwxr-xr-x 3 root root 17 Sep 15 14:17 lib64
drwxr-xr-x 2 root root 6 Nov 3 2020 libexec
lrwxrwxrwx 1 root root 35 Dec 3 16:18 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 129 Dec 3 16:17 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
//修改属主属组
[root@c193fcb41eae local]# chown -R mysql.mysql /usr/local/mysql*
[root@c193fcb41eae local]# ls -l
total 0
drwxr-xr-x 2 root root 6 Nov 3 2020 bin
drwxr-xr-x 2 root root 6 Nov 3 2020 etc
drwxr-xr-x 2 root root 6 Nov 3 2020 games
drwxr-xr-x 2 root root 6 Nov 3 2020 include
drwxr-xr-x 2 root root 6 Nov 3 2020 lib
drwxr-xr-x 3 root root 17 Sep 15 14:17 lib64
drwxr-xr-x 2 root root 6 Nov 3 2020 libexec
lrwxrwxrwx 1 mysql mysql 35 Dec 3 16:18 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Dec 3 16:17 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x 5 root root 49 Sep 15 14:17 share
drwxr-xr-x 2 root root 6 Nov 3 2020 src
//添加环境变量
[root@c193fcb41eae local]# echo export PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh
[root@c193fcb41eae local]# source /etc/profile.d/mysql.sh
[root@c193fcb41eae local]# which mysql
/usr/local/mysql/bin/mysql
//头文件(include)、读取lib
[root@c193fcb41eae local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@c193fcb41eae local]# echo /usr/local/mysql/lib > /etc/ld.so.conf.d/mysql.conf
[root@c193fcb41eae local]# ldconfig
//创建数据存放目录
[root@c193fcb41eae local]# mkdir -p /opt/data
[root@c193fcb41eae local]# chown -R mysql.mysql /opt/data/
[root@c193fcb41eae local]# ls -l /opt/data/
total 0
//初始化数据库
[root@c193fcb41eae local]# /usr/local/mysql/bin/mysqld –initialize-insecure –user=mysql –datadir=/opt/data
2021-12-03T16:29:44.502079Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T16:29:44.673278Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T16:29:44.703121Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T16:29:44.759550Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 395770aa-5456-11ec-a2ae-0242ac110003.
2021-12-03T16:29:44.761730Z 0 [Warning] Gtid table is not ready to be used. Table mysql.gtid_executed cannot be opened.
2021-12-03T16:29:45.276899Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T16:29:45.631478Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.
//生成配置文件
[root@c193fcb41eae local]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
//修改
[root@c193fcb41eae local]# sed -ri s#^(basedir=).*#\1/usr/local/mysql#g /usr/local/mysql/support-files/mysql.server
[root@c193fcb41eae local]# sed -ri s#^(datadir=).*#\1/opt/data#g /usr/local/mysql/support-files/mysql.server
[root@c193fcb41eae local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
//启动
[root@c193fcb41eae local]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to /opt/data/c193fcb41eae.err.
SUCCESS!
[root@c193fcb41eae local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
//修改密码
[root@c193fcb41eae local]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 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> set password = password(123456);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//测试登录
[root@c193fcb41eae local]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 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> quit
Bye
[root@c193fcb41eae src]# rm -rf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz