欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Docker安装MySQL

程序员文章站 2022-03-04 18:38:46
...

微信技术交流:在Docker安装MySQL

一、使用阿里云安装Docker
1、删除系统自带的Docker及依赖:

yum remove docker* container-selinux

2、添加软件源:

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3、添加必要的依赖:

yum install -y yum-utils device-mapper-persistent-data lvm2

4、更新并安装Docker-CE:

yum makecache fast
yum -y install docker-ce

5、启动Docker服务:

systemctl start docker

6、加载Docker镜像:

docker load <./mysql_centos7-201806.tar

7、标记本地Docker镜像:

docker tag 7d0b68af5a06 mysql/centos7:latest`

二、操作系统配置
1、关闭SELINUX和防火墙:

cat /etc/sysconfig/selinux

2、关闭防火墙:

systemctl stop firewalld.service
systemctl disable firewalld.service

3、将I/O调度修改为deadline模式:

dmesg|grep -i scheduler    --查看IO支持的调度
echo deadline > /sys/block/sda/queue/scheduler     --临时生效
grubby --update-kernel=ALL --args="elevator=deadline"    --永久生效
reboot

4、swap分区的设置(0表示极大限度使用物理内存,100则是积极地使用swap分区):

cat /proc/sys/vm/swappiness 
sysctl -a|grep swappiness

5、操作系统限制的设置:

ulimit -a
open files                      (-n) 1024          --error: too many open files
max user processes              (-u) 15684         --error: resource temporarily unavailable
cat /etc/security/limits.conf
*       soft    nproc   65535
*       hard    nproc   65535
*       soft    nofile  65535
*       hard    nofile  65535

6、关闭numa可以更好的分配内存:

三、使用Docker容器初始化MySQL环境
`1、初始化Container:

docker run -d -v /usr/local:/usr/local -v /data/mysql/mysql3306:/data/mysql/mysql3306 -v /etc/hosts:/etc/hosts -p 1021:22 --cap-add=NET_ADMIN --name mysql3306 -h mysql-node1 zst/centos7

2、启动容器:

docker start mysql3306
docker ps -a

3、连接到容器:

docker exec -it mysql3306 /bin/bash

4、检查程序或者库文件所依赖的共享库列表:

ldd /usr/local/mysql/bin/mysqld
yum search libaio
yum install libaio numactl

5、下载MySQL介质:

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
md5sum mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

6、创建用户:

groupadd mysql
useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -MN mysql

7、创建软链接并授权:

tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 
cd /usr/local/mysql
ln -s mysql-5.7.22-linux-glibc2.12-x86_64 mysql
chown -R mysql:mysql mysql

8、配置环境变量:

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

9、创建数据库目录:

mkdir -p /data/mysql/mysql3306/{data,log,tmp}
chown -R mysql:mysql /data/mysql

10、创建数据库配置文件:

vi /etc/my.cnf
[client]
port            = 3306
[mysql]
auto-rehash
prompt="\\aaa@qq.com\\h [\\d]>"
[mysqld]
user                                =mysql                          #   mysql
basedir                             =/usr/local/mysql/              #   /usr/local/mysql/
datadir                             =/data/mysql/mysql3306/data     #   /usr/local/mysql/data
server_id                           =1003306                        #   0
port                                =3306                           #   3306
character_set_server                =utf8                           #   latin1
explicit_defaults_for_timestamp     =off                            #    off
log_timestamps                      =system                         #   utc
socket                              =/tmp/mysql.sock                #   /tmp/mysql.sock
read_only                           =0                              #   off
log_bin                             =mysql-bin                      #   off
binlog_rows_query_log_events        =on                             #   off
log_slave_updates                   =on                             #   off
expire_logs_days                    =7                              #   0
sync_binlog                         =1                              #   1
log_error                           =error.log                        # /usr/local/mysql/data/localhost.localdomain.err

11、初始化MySQL数据库:

/usr/local/mysql/bin/mysqld --initialize &

12、启动MySQL数据库:

/usr/local/mysql/bin/mysqld &

13、查看数据库初始化密码:

grep "password" error.log

14、登陆数据库并修改密码:

mysql --verbose --help
mysql -S /tmp/mysql.sock -p
alter user user() identified by 'root123';

15、多实例环境下配置MySQL配置文件:

cp /etc/my.cnf /data/mysql/mysql3306/my3306.cnf

16、关闭数据库

mysqladmin -S /tmp/mysql.sock -p shutdown

17、重新启动数据库

/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &`
相关标签: MySQL