正文

启动脚本 文件名为mysql8.0.21.sh

#!/bin/sh
 docker run 
 -p 3307:3306 
 –name mysql 
 –privileged=true 
 –restart unless-stopped 
 -v /home/summer/mysql8/mysql:/etc/mysql 
 -v /home/summer/mysql8/logs:/logs 
 -v /home/summer/mysql8/data:/var/lib/mysql 
 -v /etc/localtime:/etc/localtime 
 -e MYSQL_ROOT_PASSWORD=hadoop 
 -d mysql:8.0.21

命令解释: -p 端口映射

–privileged=true 挂载文件权限设置

–restart unless-stopped 设置 开机后自动重启容器

-v /home/summer/mysql8/mysql:/etc/mysql 挂载配置文件

-v /home/summer/mysql8/logs:/logs \ 挂载日志

-v /home/summer/mysql8/data:/var/lib/mysql \ 挂载数据文件 持久化到主机,

-v /etc/localtime:/etc/localtime 容器时间与宿主机同步

-e MYSQL_ROOT_PASSWORD=hadoop 设置密码

-d mysql:8.0.21 后台启动,mysql

执行脚本 启动镜像

大功告成,挂载出来了数据文件以及配置文件实现数据持久化

ALTER USER “root”@“localhost” IDENTIFIED BY “hadoop”;
 FLUSH PRIVILEGES;
 create user summer@‘%’ identified by ‘123456’;
 grant all privileges on . to summer@‘%’ with grant option;
 exit;
 use mysql;
 update user set host = ‘%’ where user =‘summer’;
 ALTER USER ‘summer’@‘%’ IDENTIFIED BY ‘password’ PASSWORD EXPIRE NEVER;
 ALTER USER ‘summer’@‘%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
 exit;

方式二:正常tar包安装

上传安装包并解压

root in summer in /home/soft
❯ ll
total 473712
-r-------- 1 root root 485074552 May 17 09:51 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
root in summer in /home/soft
➜ du -sh *
463M mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
root in summer in /home/soft
➜ tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

移动重命名

root in summer in /home/soft took 2m
➜ ll
total 473716
drwxr-xr-x 9 root root 4096 May 17 10:43 mysql-8.0.19-linux-glibc2.12-x86_64
-r-------- 1 root root 485074552 May 17 09:51 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
root in summer in /home/soft
➜ mv mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/
root in summer in /home/soft
➜ cd /usr/local/ && mv mysql-8.0.19-linux-glibc2.12-x86_64/ mysql
root in summer in /usr/local
➜ ll
total 60
drwxr-xr-x 9 nobody nobody 4096 Apr 25 10:27 ats
drwxr-xr-x. 3 root root 4096 Apr 25 12:14 bin
drwxr-xr-x. 4 root root 4096 Apr 25 12:12 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 5 root root 4096 Apr 25 10:51 include
drwxr-xr-x. 5 root root 4096 Apr 25 10:51 lib
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 4096 Apr 11 2018 libexec
drwxr-xr-x 2 root root 4096 Apr 25 10:27 man
drwxr-xr-x 9 root root 4096 May 17 10:43 mysql
lrwxrwxrwx 1 root root 20 Apr 25 10:52 python374 -> /usr/local/python377
drwxr-xr-x 6 root root 4096 Apr 25 10:52 python377
drwxr-xr-x. 2 root root 4096 Apr 25 10:51 sbin
drwxr-xr-x. 7 root root 4096 Apr 25 10:51 share
drwxr-xr-x. 2 root root 4096 Apr 11 2018 src
drwxr-xr-x 3 root root 4096 Apr 25 10:51 var
root in summer in /usr/local

新增mysql用户

root in summer in /usr/local
➜ cd mysql/
root in summer in /usr/local/mysql
➜ ll
total 432
drwxr-xr-x 2 7161 31415 4096 Dec 10 2019 bin
drwxr-xr-x 2 7161 31415 4096 Dec 10 2019 docs
drwxr-xr-x 3 7161 31415 4096 Dec 10 2019 include
drwxr-xr-x 6 7161 31415 4096 Dec 10 2019 lib
-rw-r–r-- 1 7161 31415 405571 Dec 10 2019 LICENSE
drwxr-xr-x 4 7161 31415 4096 Dec 10 2019 man
-rw-r–r-- 1 7161 31415 687 Dec 10 2019 README
drwxr-xr-x 28 7161 31415 4096 Dec 10 2019 share
drwxr-xr-x 2 7161 31415 4096 Dec 10 2019 support-files
root in summer in /usr/local/mysql
➜ mkdir data
root in summer in /usr/local/mysql
➜ groupadd mysql
root in summer in /usr/local/mysql
➜ useradd -g mysql mysql
root in summer in /usr/local/mysql
➜ chown -R mysql:mysql /usr/local/mysql/
root in iscloud163-200 in /usr/local/etc
❯ mkdir /var/log/mariadb
root in iscloud163-200 in /usr/local/etc
➜ touch /var/log/mariadb/mariadb.log
root in iscloud163-200 in /usr/local/etc
➜ chown -R mysql:mysql /var/log/mariadb/

初始化

root in summer in /usr/local/mysql
➜ /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-05-17T02:46:09.950578Z 0 [Warning] [MY-011070] [Server] ‘Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it’ is deprecated and will be removed in a future release.
2021-05-17T02:46:09.950757Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.19) initializing of server in progress as process 1868276
2021-05-17T02:46:18.144914Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: bkXul9__qP*8

修改配置文件
  • 注意skip-grant-tables该参数为修改root密码

root in summer in ~
➜ cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=23306
user=mysql
socket=/tmp/mysql.sock

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

Settings user and group are ignored when systemd is used.

If you need to run mysqld under a different user or group,

customize your systemd unit file for mariadb according to the

instructions in http://fedoraproject.org/wiki/Systemd

#skip-grant-tables

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

include all files from the config directory

!includedir /etc/my.cnf.d

添加系统服务

root in summer in /usr/local/mysql
❯ cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
root in summer in /usr/local/mysql
➜ chmod +x /etc/rc.d/init.d/mysqld
root in summer in /usr/local/mysql
➜ chkconfig --add mysqld

配置环境变量

❯ vim /etc/profile
root in summer in /usr/local/mysql took 16s
➜ source /etc/profile
root in summer in /usr/local/mysql
❯ ln -s /usr/local/mysql/bin/mysql /usr/bin
root in summer in /usr/local/mysql
➜ ln -s /usr/local/mysql/bin/mysqld /usr/bin

定义root密码

root in summer in /usr/local/mysql
➜ /etc/init.d/mysqld restart