二进制部署MySQL 5.7

一、获取MySQL软件

企业版:Enterprise  , 互联网行业一般不选择.

社区版本:选择
            源码包 编译安装:  source code    .tar.gz   
            通用二进制 
  

公司用什么版本数据库? 具体什么小版本号?
5.6.20 5.6.34 5.6.36  5.6.38  5.6.40  
5.7.18  5.7.20  5.7.22 5.7.44

通用二进制版本: 本文档采用此方式安装

https://dev.mysql.com/downloads/mysql/

点击“Looking for previous GA versions?”,跳转后依次选择“Linux-Generic”“Linux-Generic(glibc2.12)(x86,64-bit)”

image-20240312100634072

image-20240312100650835

源码包 版本:过于复杂,一般有自定义需求才采用此方式

https://dev.mysql.com/downloads/mysql/

点击“Looking for previous GA versions?”,跳转后依次选择“Source Code”“All Operating Systems(Generic)”

image-20240312100714878

image-20240312100726734

二、MySQL二进制安装

备注: !!!!!!!!!!!!!!!!!部署前需要先 yum remove mariadb

2.1、创建mysql工作目录:

[root@db01 ~]# mkdir -p /home/application/mysql

2.2、上传软件,并解压并改名为app

[root@db01 app]# tar -xf /root/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz

[root@db01 app]# mv mysql-5.7.39-linux-glibc2.12-x86_64 /home/application/mysql/app
[root@db01 app]# ls -l /home/application/mysql/app
total 36
drwxr-xr-x  2 root root   4096 Mar  4 14:55 bin
-rw-r--r--  1 7161 31415 17987 Sep 13  2017 COPYING
drwxr-xr-x  2 root root     55 Mar  4 14:55 docs
drwxr-xr-x  3 root root   4096 Mar  4 14:55 include
drwxr-xr-x  5 root root    229 Mar  4 14:55 lib
drwxr-xr-x  4 root root     30 Mar  4 14:55 man
-rw-r--r--  1 7161 31415  2478 Sep 13  2017 README
drwxr-xr-x 28 root root   4096 Mar  4 14:55 share
drwxr-xr-x  2 root root     90 Mar  4 14:55 support-files

2.3、修改环境变量:

[root@db01 app]# vim /etc/profile #加入一行
export PATH=$PATH:/home/application/mysql/app/bin

[root@db01 bin]# source /etc/profile

2.4、建立mysql用户和组(如果有可忽略)

useradd -s /sbin/nologin mysql -M

2.5、创建mysql 数据目录,日志目录;并修改权限

mkdir -p /home/application/mysql/data
mkdir -p /home/application/mysql/logs

chown -Rf mysql.mysql /home/application/mysql

2.6、准备my.cnf 配置文件

[mysqld]

user=mysql
basedir=/home/application/mysql/app
datadir=/home/application/mysql/data
default-storage-engine=INNODB


character-set-server=utf8mb4
collation_server = utf8mb4_general_ci

#只能用IP地址检查客户端的登录,不用主机名,跳过域名解析
skip-name-resolve=1
 
#日志时间
log_timestamps=SYSTEM
default-time-zone = '+8:00'
 
 
#慢日志
long_query_time=3
slow_query_log=ON
slow_query_log_file=/home/application/mysql/logs/slow_query.log
#不记录未使用索引的查询到慢查询日志中
log_queries_not_using_indexes = 0
#管理员执行的慢查询语句记录到慢查询日志中
log_slow_admin_statements = 1
#将从服务器执行的慢查询语句记录到慢查询日志中
log_slow_slave_statements = 1
#当未使用索引的查询数量达到10次时,开始将这些查询记录到慢查询日志中
log_throttle_queries_not_using_indexes = 10
 

#通用日志
#general_log=1
#general_log_file=/home/application/mysql/logs/mysql_general.log
 
#错误日志
log-error=/home/application/mysql/logs/mysqld.log
 
#innodb配置
innodb_file_per_table = 1
innodb_data_file_path = ibdata1:500M:autoextend:max:20G
innodb_temp_data_file_path = ibtmp1:500M:autoextend:max:20G



#binlog配置
server_id=17
log-bin=mysql-bin
max_binlog_size = 100M
binlog_format=row
log_slave_updates
expire_logs_days=7


# disable_ssl
skip_ssl
port=3306
socket=/tmp/mysql.sock
max_connections=1000
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet=512M
 
[mysql]
socket=/tmp/mysql.sock
default-character-set = utf8mb4


[client]
default-character-set = utf8mb4

2.7、初始化数据(建库)

初始化数据,初始化管理员的密码为空

# 如果已经初始化过了,就需要把数据目录数据情况,再次初始化 rm -rf  /home/application/mysql/data/*

[root@db01 ~]# mysqld --defaults-file=/etc/my.cnf --initialize-insecure

[root@openeuler mysql]# ls -l /home/application/mysql/data/

总用量 610376
-rw-r----- 1 mysql mysql        56  3月 12 13:23 auto.cnf
-rw------- 1 mysql mysql      1676  3月 12 13:23 ca-key.pem
-rw-r--r-- 1 mysql mysql      1112  3月 12 13:23 ca.pem
-rw-r--r-- 1 mysql mysql      1112  3月 12 13:23 client-cert.pem
-rw------- 1 mysql mysql      1676  3月 12 13:23 client-key.pem
-rw-r----- 1 mysql mysql       436  3月 12 13:23 ib_buffer_pool
-rw-r----- 1 mysql mysql 524288000  3月 12 13:23 ibdata1
-rw-r----- 1 mysql mysql  50331648  3月 12 13:23 ib_logfile0
-rw-r----- 1 mysql mysql  50331648  3月 12 13:23 ib_logfile1
drwxr-x--- 2 mysql mysql      4096  3月 12 13:23 mysql
-rw-r----- 1 mysql mysql       177  3月 12 13:23 mysql-bin.000001
-rw-r----- 1 mysql mysql        19  3月 12 13:23 mysql-bin.index
drwxr-x--- 2 mysql mysql      4096  3月 12 13:23 performance_schema
-rw------- 1 mysql mysql      1680  3月 12 13:23 private_key.pem
-rw-r--r-- 1 mysql mysql       452  3月 12 13:23 public_key.pem
-rw-r--r-- 1 mysql mysql      1112  3月 12 13:23 server-cert.pem
-rw------- 1 mysql mysql      1676  3月 12 13:23 server-key.pem
drwxr-x--- 2 mysql mysql     12288  3月 12 13:23 sys
[root@openeuler mysql]# 

2.8、使用systemd管理mysql

[root@db01 ~]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
 
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/home/application/mysql/app/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

2.9、systemd 管理相关命令

systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld

2.10、修改root密码

[root@db01 ~]# mysqladmin -uroot -p password xxxxxx
Enter password:   直接回车
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.11、使用新密码登录

2.11、使用新密码登录

[root@db01 ~]# mysql -uroot -p

三、常见问题

3.1 问题1:

mysqld --initialize-user=mysql --basedir=/app/mysqlmyseld: error while loading shared libraries: libaio.so.1: cannot open shared obiect file: No suchor directory

image-20240312144334108

报错原因: Linux系统中缺少libaio-devel 软件包

解决:
yum install -y libaio-devel

3.2 问题2:

mysql登录报错,mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory**

find /usr/ -name 'libtinfo*'

ln -s /usr/lib64/libtinfo.so.6.3 /usr/lib64/libtinfo.so.5

3.3 问题3:

[ERROR] --initialize specified but the data directory has files in it

image-20240312144605957

报错原因:
在/data/mysql 存在文件

解决: 删除数据目录
rm -rf /home/application/mysql/data/*