容器环境的MySQL手动搭建

HostName
localhost宿主机
master容器
## 1、准备工作

查询master节点是否安装mariadb(centos可能会自带)

[root@master /]# rpm -qa | grep mariadb

卸载mariadb(有才卸)

[root@master /]# rpm -e --nodeps mariadb-libs
[root@master /]# rpm -qa | grep mariadb

2、拉取压缩包

发送宿主机压缩包至容器

[root@localhost software]# docker cp mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz master:/opt/

将压缩包解压至/usr/local下

[root@master opt]# tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local

查看/usr/local下的文件夹名

[root@master opt]# ls /usr/local | grep mysql
mysql-5.7.18-linux-glibc2.5-x86_64

修改文件夹名称

[root@master opt]# mv /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql

3、文件夹操作

创建mysql用户和组

[root@master opt]# groupadd mysql
[root@master opt]# useradd -r -g mysql mysql

创建mysql数据文件和临时文件目录

[root@master opt]# mkdir -p /usr/local/mysql/{data,tmpdir}

更改mysql文件件目录及子目录的文件所属和所属组

[root@master opt]# chown -R mysql:mysql /usr/local/mysql
#查看目录权限
[root@master opt]# ll /usr/local/mysql
total 36
-rw-r--r--.  1 mysql mysql 17987 Mar 18  2017 COPYING
-rw-r--r--.  1 mysql mysql  2478 Mar 18  2017 README
drwxr-xr-x.  2 mysql mysql  4096 Sep 13 02:31 bin
drwxr-xr-x.  2 mysql mysql     6 Sep 13 02:37 data
drwxr-xr-x.  2 mysql mysql    55 Sep 13 02:31 docs
drwxr-xr-x.  3 mysql mysql  4096 Sep 13 02:29 include
drwxr-xr-x.  5 mysql mysql   229 Sep 13 02:31 lib
drwxr-xr-x.  4 mysql mysql    30 Sep 13 02:30 man
drwxr-xr-x. 28 mysql mysql  4096 Sep 13 02:31 share
drwxr-xr-x.  2 mysql mysql    90 Sep 13 02:31 support-files
drwxr-xr-x.  2 mysql mysql     6 Sep 13 02:37 tmpdir

4、环境及初始化

配置mysql环境变量

[root@master opt]# vi /etc/profile
#在文件末尾追加以下内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
:wq
[root@master opt]# source /etc/profile

初始化mysql

[root@master local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2022-09-13T03:39:09.632035Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-09-13T03:39:12.693482Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-09-13T03:39:12.795241Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-09-13T03:39:12.853653Z 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: a24b7178-3315-11ed-95be-0242ac120065.
2022-09-13T03:39:12.854669Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-09-13T03:39:12.855587Z 1 [Note] A temporary password is generated for root@localhost: _/f-Q/jZu6*L

5、新建MySQL配置文件

保存上步骤中行尾出现在末尾的 _/f-Q/jZu6*L,这是mysql自动生成的默认密码,需要暂时保存。

创建my.cnf文件并编辑

[root@master /]# vi /etc/my.cnf
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8
# 使用自动补齐功能
auto-rehash
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 临时目录地址
tmpdir=/usr/local/mysql/tmpdir
#设置socke文件地址
socket=/tmp/mysql.sock
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 是否对sql语句大小写敏感,默认值0,1表示不敏感
lower_case_table_names=1
max_allowed_packet=16M

6、系统操作

将mysql服务添加到系统服务列表

[root@master /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@master /]# chmod +x /etc/init.d/mysqld

启动mysql,设置开机自启

[root@master /]# /etc/init.d/mysqld start 
Starting MySQL......Logging to '/usr/local/mysql/data/master.err'.
... SUCCESS! 
[root@master /]# chkconfig --level 35 mysqld on
[root@master /]# ps -ef | grep mysql*

7、修改用户密码添加远程权限

[root@master /]# mysql -uroot -p'_/f-Q/jZu6*L'
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.18

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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('password');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
+-----------+-----------+
2 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit;
Bye

测试mysql密码

[root@master /]# mysql -uroot -ppassword
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 4
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> create database test;
Query OK, 1 row affected (0.00 sec)

踩坑:

1、初始化报错:

mysqld: error while loading shared libraries: ![img](file:///C:\Users\Administrator\AppData\Roaming\Tencent\QQTempSys\%W@GJ$ACOF(TYDYECOKVDYB.png)libaio.so.1: 
cannot open shared object file: No such file or directory

问题分析:
安装mysql,进行初始化时失败,报错误提示

解决方案:
rpm -qa | grep libaio
yum install -y libaio

2、没有systemctl指令,导致启动不了服务

如果使用Centos镜像,在docker run的时候应赋予管理员权限以及/var/sbin/init的工作目录。

3、启动报错

MySQL启动报错: Failed to start LSB: start and stop MySQL.与连接而数据库报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql/mysql.sock’ (2)

主要是权限问题,看目录所属分组是否为mysql,实在不行将tmp整个文件夹chmod -R 777

查看my.cnf的socket参数,看看路径

posted @ 2024-01-17 02:24  Shachar_xc  阅读(42)  评论(0)    收藏  举报