MySQL 数据库搭建

复制和安装 mysql

  1. 将本地mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar复制进master容器中root目录中
1
docker cp /path/to/mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar master:/root
  1. mysql的 tar 包解压到当前目录中
1
tar xf mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar
  • 解压完毕后会得到以下 rpm 包
1
2
3
4
5
6
7
8
9
10
mysql-community-client-5.7.44-1.el7.x86_64.rpm
mysql-community-common-5.7.44-1.el7.x86_64.rpm
mysql-community-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-server-5.7.44-1.el7.x86_64.rpm
mysql-community-test-5.7.44-1.el7.x86_64.rpm

下载依赖并安装 rpm 包

  1. 下载依赖
1
yum install perl net-tools libaio libnuma
  1. 安装相应的 rpm 包
1
2
3
4
rpm -ivh mysql-community-common-5.7.44-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.44-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.44-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.44-1.el7.x86_64.rpm

配置并运行 mysql

  1. 初始化
1
mysqld --initialize
  1. 查询 mysql 密码
1
tail /var/log/mysqld.log
  • 某一行是这样的就是密码
1
2024-06-30T02:52:23.822524Z 1 [Note] A temporary password is generated for root@localhost: <l9Ml9CmtyjU
  1. 修改数据库目录的所属用户及其所属组
1
chown mysql:mysql /var/lib/mysql -R
  1. 启动 mysql 数据库
1
systemctl start mysqld
1
systemctl enable mysqld

进入 mysql 并修改密码和权限

  1. 运行
1
mysql -u root -p'<l9Ml9CmtyjU'
  1. 修改密码策略和密码长度
1
2
set global validate_password_policy=0;
set global validate_password_length=4;
  1. 修改 root 密码
1
ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
  1. 赋予 root 所有权限
1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1234' WITH GRANT OPTION;
  1. 刷新权限
1
FLUSH PRIVILEGES;
  1. 重启 MySQL
1
systemctl restart mysqld