1.初识数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
数据库管理系统
DBMS(database management system)
-管理数据
-存储数据

#数据库管理系统类型
1.关系型数据库 RDBMS
-所有的表都是有二维表组成
-表与表之间有关联
-使用SQL(结构化查询语言)语句来管理
2.非关系型数据库NoSQL
-不需要使用SQL语句,没有二维表

#关系型数据库和非关系型数据库常见的库
关系型: MySQL、mariadb、Oracle、PostgreSQL、MSSQL
非关系型:Redis(键值对)、MongoDB(文档型数据库)、elasticsearch(搜索引擎数据库)、memcached(键值对)
InfluxDB:Time Series 时序数据库(适合出图,查询时间轴数据方便)
OpenTSDB:Time Series 时序数据库(适合出图,查询时间轴数据方便)
etcd:键值对数据库(配置文件管理中心)

#MySQL安装方式
1.yum安装
2.二进制安装
3.源码安装
4.docker容器


root@(none) > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linux50 |
| mysql |
| performance_schema |
| student |
| test |
| world |
| zrlog |
+--------------------+
8 rows in set (0.21 sec)


#存放MySQ所有元数据的库,并且在磁盘不显示
information_schema

数据库存储服务概述

1
今时今⽇,数据库系统已经成为各个动态⽹站上 web 应⽤程序的重要组成部分。 由于⾮常敏感和机密的数据有可能保存在数据库中,所以对数据库实施保护就显得尤为重要了。 要从数据库中提取或者存⼊数据,就必须经过连接数据库、发送⼀条合法查询、获取结果、关闭连接等步骤。 ⽬前,能完成这⼀系列动作的最常⽤的查询语⾔是结构化查询语⾔ Structured Query Language (SQL)。 并且在对数据库进⾏管理与维护的过程中,需要对数据库的相关知识进⾏充分的掌握,最终才能保证企业核⼼数据的安全性。

获取 MySQL

官方:https://www.mysql.com/

1
2
3
4
MySQL5.6:GA 6-12个月,小版本号为偶数版的
MySQL5.7:GA 6-12个月,小版本号为偶数版的,5.7.20以上版本(MGR MySQL自带的高可用功能)

获取的包bug少

240807 162316

QQ_1723024541340

字符集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#查询字符集

root@(none) > show collation;
+--------------------------+----------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------------+----------+-----+---------+----------+---------+
| big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
| big5_bin | big5 | 84 | | Yes | 1 |
| dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
| dec8_bin | dec8 | 69 | | Yes | 1 |
| cp850_general_ci | cp850 | 4 | Yes | Yes | 1 |
| cp850_bin | cp850 | 80 | | Yes | 1 |

#校验规则
1)ci:大小写不敏感
2)cs或bin:大小写敏感

## 查看当前系统的字符集
[root@db04 ~]# echo $LANG
en_US.UTF-8

## 临时修改系统字符集
[root@db04 ~]# export LANG=en_US.UTF-8

## 永久修改 CentOS7
[root@db04 ~]# vim /etc/locale.conf
LANG="en_US.UTF-8"

## 永久修改 CentOS6
[root@db04 ~]# vim /etc/sysconfig/i18n
LANG="en_US.UTF-8


应用修改字符集
[root@db04 ~]# vim /etc/my.cnf
[mysqld]
character_set_server=utf8

mysql> show create database mysql;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| mysql | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
mysql> alter database mysql charset utf8;

#查询所有不是utf8的字符集
mysql> select * from information_schema.tables where table_collation not like '%utf8%';

### 修改单张表字符集
mysql> alter table zls.test111 charset utf8;

### 批量修改表的字符集
## 方法一:
[root@db04 ~]# for n in `seq 7`;do mysql -uroot -p123 -e "alter table
zabbix.zabbix$n charset utf8" ;done
## 方法二:
[root@db04 ~]# mysqldump -uroot -p123 -B zabbix > /tmp/zabbix.sql
[root@db04 ~]# vim /tmp/zabbix.sql
:%s#CHARSET=latin1#CHARSET=utf8#g

[root@db04 ~]# sed -i 's#CHARSET=latin1#CHARSET=utf8#g' /tmp/zabbix.sql
[root@db04 ~]# mysql -uroot -p123 < /tmp/zabbix.sql

2.安装MySQL

源码安装MySQL5.6

以5.6.50版本为例

1
2
3
4
5
6
7
8
#获取mysql包
#源码安装
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.50.tar.gz

#二进制安装
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz

#国外网站下载慢,有条件开魔法

编译 && 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#0.创建安装目录
mkdir /app

#1.安装依赖
yum install -y cmake gcc gcc-c++ git openssl-devel ncurses-devel autoconf libaio-devel

#2.解压
tar xf mysql-5.6.50.tar.gz

#3.生成 ./configure --prefix=xxx cmake(这一步需要cmake依赖,和C环境)
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql-5.6.50 \
-DMYSQL_DATADIR=/app/mysql-5.6.50/data \
-DMYSQL_UNIX_ADDR=/app/mysql-5.6.50/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

#####注:
如果遇到报错,下次cmake之前一定要删除CMakeCache.txt文件(执行cmake都会产生)
如果是缺少gcc等环境报错,安装完毕c环境之后,要删除整个包在重新解压再去重,cmake



#程序存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql-5.6.50 \
#数据存放位置
-DMYSQL_DATADIR=/app/mysql-5.6.50/data \
#socket文件存放位置
-DMYSQL_UNIX_ADDR=/app/mysql-5.6.50/tmp/mysql.sock \
#使用utf8字符集
-DDEFAULT_CHARSET=utf8 \
#校验规则
-DDEFAULT_COLLATION=utf8_general_ci \
#使用其他额外的字符集
-DWITH_EXTRA_CHARSETS=all \
#支持的存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#禁用的存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
#启用zlib库支持(zib、gzib相关)
-DWITH_ZLIB=bundled \
#启用SSL库支持(安全套接层)
-DWITH_SSL=bundled \
#启用本地数据导入支持
-DENABLED_LOCAL_INFILE=1 \
#编译嵌入式服务器支持
-DWITH_EMBEDDED_SERVER=1 \
# mysql5.6支持了google的c++mock框架了,允许下载,否则会安装报错。
-DENABLE_DOWNLOADS=1 \
#禁用debug(默认为禁用)
-DWITH_DEBUG=0

# 4.编译和安装
make && make install

安装数据库

和二进制安装一样步骤,报错不一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#1.拷贝启动脚本到init.d下
cp /app/mysql-5.6.50/support-files/mysql.server /etc/init.d/mysqld

# 2.创建MySQL程序用户
useradd mysql -s /sbin/nologin -M

#3.初始化mysql目录
/app/mysql-5.6.50/scripts/mysql_install_db --user=mysql --basedir=/app/mysql-5.6.50 --datadir=/app/mysql-5.6.50/data

###########################
--user 指定用户
--basedir 指定安装目录
--datadir 编译的时候指定的data目录
###########################

[root@db01 mysql-5.6.50]# echo $?
0

#4.启动数据库
/etc/init.d/mysqld start #(报错原因见--启动报错解决)

#5.正确启动流程前提(注意是否有mariadb数据库)
mkdir /app/mysql-5.6.50/tmp
chown -R mysql.mysql /app/mysql-5.6.50/

#检查是否有mariadb并删除
[root@db01 mysql-5.6.50]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@db01 mysql-5.6.50]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[root@db01 mysql-5.6.50]#

#再次执行启动命令
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

#6.设置开机启动
chkconfig --add mysqld

chkconfig --list
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

#7.连接数据库(为啥需要添加环境变量详情见--连接mysql报错)
#添加环境变量
echo 'export PATH="/app/mysql-5.6.50/bin:$PATH"' >/etc/profile.d/mysql.sh

#加载环境变量
source /etc/profile

#使用mysql就可以愉快连接数据库了
[root@db01 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.50 Source distribution
Copyright (c) 2000, 2020, 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>

启动数据库报错解决

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/app/mysql-5.6.50/data/db01.err'.
240807 14:03:01 mysqld_safe Directory '/app/mysql-5.6.50/tmp' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/app/mysql-5.6.50/data/db01.pid).

#报错分析:
ERROR! The server quit without updating PID file (/app/mysql-5.6.50/data/db01.pid).
#没有pid文件,为啥没有?
只有程序启动才会有pid文件,说明mysql根本就没有正常启动

#往上分析
240807 14:03:01 mysqld_safe Directory '/app/mysql-5.6.50/tmp' for UNIX socket file don't exists.
#socket 文件不存在?

QQ_1723015942442

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
报错分析:
编译的时候指定socket文件存放在此处,但是没有这个目录
解决:
mkdir /app/mysql-5.6.50/tmp

#再次执行启动命令
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/app/mysql-5.6.50/data/db01.err'.
... ERROR! The server quit without updating PID file (/app/mysql-5.6.50/data/db01.pid).

#分析:看日志
[root@db01 ~]# cat /app/mysql-5.6.50/data/db01.err
2024-08-07 15:04:15 24317 [Note] Plugin 'FEDERATED' is disabled.
2024-08-07 15:04:15 24317 [Note] InnoDB: Using atomics to ref count buffer pool pages
2024-08-07 15:04:15 24317 [Note] InnoDB: The InnoDB memory heap is disabled
2024-08-07 15:04:15 24317 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2024-08-07 15:04:15 24317 [Note] InnoDB: Memory barrier is not used
2024-08-07 15:04:15 24317 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-08-07 15:04:15 24317 [Note] InnoDB: Using Linux native AIO
2024-08-07 15:04:15 24317 [Note] InnoDB: Using CPU crc32 instructions
2024-08-07 15:04:15 24317 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2024-08-07 15:04:15 24317 [Note] InnoDB: Completed initialization of buffer pool
2024-08-07 15:04:15 24317 [Note] InnoDB: Highest supported file format is Barracuda.
2024-08-07 15:04:15 24317 [Note] InnoDB: 128 rollback segment(s) are active.
2024-08-07 15:04:15 24317 [Note] InnoDB: Waiting for purge to start
2024-08-07 15:04:15 24317 [Note] InnoDB: 5.6.50 started; log sequence number 1625987
2024-08-07 15:04:15 24317 [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: 421a7ae9-548b-11ef-aa26-000c292b15c7.
2024-08-07 15:04:15 24317 [Note] RSA private key file not found: /app/mysql-5.6.50/data//private_key.pem. Some authentication plugins will not work.
2024-08-07 15:04:15 24317 [Note] RSA public key file not found: /app/mysql-5.6.50/data//public_key.pem. Some authentication plugins will not work.
2024-08-07 15:04:15 24317 [Note] Server hostname (bind-address): '*'; port: 3306
2024-08-07 15:04:15 24317 [Note] IPv6 is available.
2024-08-07 15:04:15 24317 [Note] - '::' resolves to '::';
2024-08-07 15:04:15 24317 [Note] Server socket created on IP: '::'.
2024-08-07 15:04:15 24317 [ERROR] Can't start server : Bind on unix socket: Permission denied
2024-08-07 15:04:15 24317 [ERROR] Do you already have another mysqld server running on socket: /app/mysql-5.6.50/tmp/mysql.sock ?
2024-08-07 15:04:15 24317 [ERROR] Aborting

2024-08-07 15:04:15 24317 [Note] Binlog end
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'partition'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_METRICS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMPMEM'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_CMP'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_LOCKS'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'INNODB_TRX'
2024-08-07 15:04:15 24317 [Note] Shutting down plugin 'InnoDB'
2024-08-07 15:04:15 24317 [Note] InnoDB: FTS optimize thread exiting.
2024-08-07 15:04:15 24317 [Note] InnoDB: Starting shutdown...
2024-08-07 15:04:17 24317 [Note] InnoDB: Shutdown completed; log sequence number 1625997
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'BLACKHOLE'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'ARCHIVE'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'MRG_MYISAM'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'MyISAM'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'MEMORY'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'CSV'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'sha256_password'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'mysql_old_password'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'mysql_native_password'
2024-08-07 15:04:17 24317 [Note] Shutting down plugin 'binlog'
2024-08-07 15:04:17 24317 [Note] /app/mysql-5.6.50/bin/mysqld: Shutdown complete

QQ_1723015890661

1
2
3
4
5
6
7
8
9
Can't start server : Bind on unix socket: Permission denied
分析:权限不足
解决:
chown -R mysql.mysql /app/mysql-5.6.50/


#再次执行启动命令
[root@db01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

连接msyql报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@db01 mysql-5.6.50]# mysql
-bash: mysql: command not found
#为啥没有mysql命令?
没有添加环境变量,mysql命令在/app/mysql-5.6.50/bin下

#使用绝对路径也可以启动
[root@db01 mysql-5.6.50]# /app/mysql-5.6.50/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 Source distribution

Copyright (c) 2000, 2020, 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>




###怎么解决?
方法1.
#添加环境变量
echo 'export PATH="/app/mysql-5.6.50/bin:$PATH"' >/etc/profile.d/mysql.sh

#重新加载环境变量
source /etc/profile

方法2.
ln -s /app/mysql-5.6.50/bin/* /usr/bin/

#建议
给mysql做个软链接,假如mysql升级就不需要再次做软链接了
ln -s /app/mysql-5.6.50/ /app/mysql
ln -s /app/mysql/bin/* /usr/bin/

初始化报错解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#报错
FATAL ERROR: please install the following Perl modules before executing /app/mysql-5.6.50/scripts/mysql_install_db:
Data::Dumper
#解决方法是安装autoconf库和libaio库文件
yum -y install autoconf libaio-devel

#再次执行初始化数据库操作
[root@db01 ~]# /app/mysql-5.6.50/scripts/mysql_install_db --user=mysql --basedir=/app/mysql-5.6.50 --datadir=/app/mysql-5.6.50/data
Installing MySQL system tables...2024-08-07 14:00:39 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-07 14:00:39 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2024-08-07 14:00:39 0 [Note] /app/mysql-5.6.50/bin/mysqld (mysqld 5.6.50) starting as process 23552 ...
2024-08-07 14:00:39 23552 [Note] InnoDB: Using atomics to ref count buffer pool pages
2024-08-07 14:00:39 23552 [Note] InnoDB: The InnoDB memory heap is disabled
2024-08-07 14:00:39 23552 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2024-08-07 14:00:39 23552 [Note] InnoDB: Memory barrier is not used
2024-08-07 14:00:39 23552 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-08-07 14:00:39 23552 [Note] InnoDB: Using Linux native AIO
2024-08-07 14:00:39 23552 [Note] InnoDB: Using CPU crc32 instructions
2024-08-07 14:00:39 23552 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2024-08-07 14:00:39 23552 [Note] InnoDB: Completed initialization of buffer pool
2024-08-07 14:00:39 23552 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Database physically writes the file full: wait...
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2024-08-07 14:00:39 23552 [Warning] InnoDB: New log files created, LSN=45781
2024-08-07 14:00:39 23552 [Note] InnoDB: Doublewrite buffer not found: creating new
2024-08-07 14:00:39 23552 [Note] InnoDB: Doublewrite buffer created
2024-08-07 14:00:39 23552 [Note] InnoDB: 128 rollback segment(s) are active.
2024-08-07 14:00:39 23552 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-07 14:00:39 23552 [Note] InnoDB: Foreign key constraint system tables created
2024-08-07 14:00:39 23552 [Note] InnoDB: Creating tablespace and datafile system tables.
2024-08-07 14:00:39 23552 [Note] InnoDB: Tablespace and datafile system tables created.
2024-08-07 14:00:39 23552 [Note] InnoDB: Waiting for purge to start
2024-08-07 14:00:39 23552 [Note] InnoDB: 5.6.50 started; log sequence number 0
2024-08-07 14:00:39 23552 [Note] RSA private key file not found: /app/mysql-5.6.50/data//private_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:39 23552 [Note] RSA public key file not found: /app/mysql-5.6.50/data//public_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:40 23552 [Note] Binlog end
2024-08-07 14:00:40 23552 [Note] InnoDB: FTS optimize thread exiting.
2024-08-07 14:00:40 23552 [Note] InnoDB: Starting shutdown...
2024-08-07 14:00:41 23552 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2024-08-07 14:00:41 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-07 14:00:41 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2024-08-07 14:00:41 0 [Note] /app/mysql-5.6.50/bin/mysqld (mysqld 5.6.50) starting as process 23574 ...
2024-08-07 14:00:41 23574 [Note] InnoDB: Using atomics to ref count buffer pool pages
2024-08-07 14:00:41 23574 [Note] InnoDB: The InnoDB memory heap is disabled
2024-08-07 14:00:41 23574 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2024-08-07 14:00:41 23574 [Note] InnoDB: Memory barrier is not used
2024-08-07 14:00:41 23574 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-08-07 14:00:41 23574 [Note] InnoDB: Using Linux native AIO
2024-08-07 14:00:41 23574 [Note] InnoDB: Using CPU crc32 instructions
2024-08-07 14:00:41 23574 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2024-08-07 14:00:41 23574 [Note] InnoDB: Completed initialization of buffer pool
2024-08-07 14:00:41 23574 [Note] InnoDB: Highest supported file format is Barracuda.
2024-08-07 14:00:41 23574 [Note] InnoDB: 128 rollback segment(s) are active.
2024-08-07 14:00:41 23574 [Note] InnoDB: Waiting for purge to start
2024-08-07 14:00:41 23574 [Note] InnoDB: 5.6.50 started; log sequence number 1625977
2024-08-07 14:00:41 23574 [Note] RSA private key file not found: /app/mysql-5.6.50/data//private_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:41 23574 [Note] RSA public key file not found: /app/mysql-5.6.50/data//public_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:41 23574 [Note] Binlog end
2024-08-07 14:00:41 23574 [Note] InnoDB: FTS optimize thread exiting.
2024-08-07 14:00:41 23574 [Note] InnoDB: Starting shutdown...
2024-08-07 14:00:43 23574 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/app/mysql-5.6.50/bin/mysqladmin -u root password 'new-password'
/app/mysql-5.6.50/bin/mysqladmin -u root -h db01 password 'new-password'

Alternatively you can run:

/app/mysql-5.6.50/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; /app/mysql-5.6.50/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /app/mysql-5.6.50/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

执行完毕后 ,过程中会有两个OK
[root@db01 ~]# echo $?
0

二进制安装MySQL5.6

安装数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#0.创建安装目录
mkdir /app

#1.安装依赖
yum -y install openssl-devel ncurses-devel autoconf libaio-devel

# 2.解压
tar xf mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz -C /app

# 3.改名
mv /app/mysql-5.6.50-linux-glibc2.12-x86_64 /app/mysql-5.6.50

# 4.创建MySQL程序用户
useradd mysql -s /sbin/nologin -M

# 5.拷贝MySQL的配置文件到/etc/下
\cp /app/mysql-5.6.50/support-files/my-default.cnf /etc/my.cnf
[root@db01 mysql-5.6.50]# vim /etc/my.cnf
[mysqld]

# 6.拷贝启动脚本
cp /app/mysql-5.6.50/support-files/mysql.server /etc/init.d/mysqld

# 7.初始化MySQL--报错见初始化解决
/app/mysql-5.6.50/scripts/mysql_install_db --user=mysql --basedir=/app/mysql-5.6.50 --datadir=/app/mysql-5.6.50/data

###########################
--user 指定用户
--basedir 指定安装目录
--datadir 编译的时候指定的data目录
###########################

# 8.启动MySQL (为啥需要指定dir?--详情见二进制启动失败)
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
basedir=/app/mysql-5.6.50
datadir=/app/mysql-5.6.50/data
[root@db02 ~]# /etc/init.d/mysqld start


#9.连接mysql(为啥加环境变量见此章--源码安装mysql,连接mysql失败)
#添加环境变量
echo 'export PATH="/app/mysql-5.6.50/bin:$PATH"' >/etc/profile.d/mysql.sh

#加载环境变量
source /etc/profile

#连接数据库
msyql
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

初始化报错解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#报错
FATAL ERROR: please install the following Perl modules before executing /app/mysql-5.6.50/scripts/mysql_install_db:
Data::Dumper
#解决方法是安装autoconf库和libaio库文件
yum -y install autoconf libaio-devel

#再次执行初始化数据库操作
[root@db01 ~]# /app/mysql-5.6.50/scripts/mysql_install_db --user=mysql --basedir=/app/mysql-5.6.50 --datadir=/app/mysql-5.6.50/data
Installing MySQL system tables...2024-08-07 14:00:39 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-07 14:00:39 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2024-08-07 14:00:39 0 [Note] /app/mysql-5.6.50/bin/mysqld (mysqld 5.6.50) starting as process 23552 ...
2024-08-07 14:00:39 23552 [Note] InnoDB: Using atomics to ref count buffer pool pages
2024-08-07 14:00:39 23552 [Note] InnoDB: The InnoDB memory heap is disabled
2024-08-07 14:00:39 23552 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2024-08-07 14:00:39 23552 [Note] InnoDB: Memory barrier is not used
2024-08-07 14:00:39 23552 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-08-07 14:00:39 23552 [Note] InnoDB: Using Linux native AIO
2024-08-07 14:00:39 23552 [Note] InnoDB: Using CPU crc32 instructions
2024-08-07 14:00:39 23552 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2024-08-07 14:00:39 23552 [Note] InnoDB: Completed initialization of buffer pool
2024-08-07 14:00:39 23552 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Database physically writes the file full: wait...
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2024-08-07 14:00:39 23552 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2024-08-07 14:00:39 23552 [Warning] InnoDB: New log files created, LSN=45781
2024-08-07 14:00:39 23552 [Note] InnoDB: Doublewrite buffer not found: creating new
2024-08-07 14:00:39 23552 [Note] InnoDB: Doublewrite buffer created
2024-08-07 14:00:39 23552 [Note] InnoDB: 128 rollback segment(s) are active.
2024-08-07 14:00:39 23552 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-07 14:00:39 23552 [Note] InnoDB: Foreign key constraint system tables created
2024-08-07 14:00:39 23552 [Note] InnoDB: Creating tablespace and datafile system tables.
2024-08-07 14:00:39 23552 [Note] InnoDB: Tablespace and datafile system tables created.
2024-08-07 14:00:39 23552 [Note] InnoDB: Waiting for purge to start
2024-08-07 14:00:39 23552 [Note] InnoDB: 5.6.50 started; log sequence number 0
2024-08-07 14:00:39 23552 [Note] RSA private key file not found: /app/mysql-5.6.50/data//private_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:39 23552 [Note] RSA public key file not found: /app/mysql-5.6.50/data//public_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:40 23552 [Note] Binlog end
2024-08-07 14:00:40 23552 [Note] InnoDB: FTS optimize thread exiting.
2024-08-07 14:00:40 23552 [Note] InnoDB: Starting shutdown...
2024-08-07 14:00:41 23552 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2024-08-07 14:00:41 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-07 14:00:41 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2024-08-07 14:00:41 0 [Note] /app/mysql-5.6.50/bin/mysqld (mysqld 5.6.50) starting as process 23574 ...
2024-08-07 14:00:41 23574 [Note] InnoDB: Using atomics to ref count buffer pool pages
2024-08-07 14:00:41 23574 [Note] InnoDB: The InnoDB memory heap is disabled
2024-08-07 14:00:41 23574 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2024-08-07 14:00:41 23574 [Note] InnoDB: Memory barrier is not used
2024-08-07 14:00:41 23574 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-08-07 14:00:41 23574 [Note] InnoDB: Using Linux native AIO
2024-08-07 14:00:41 23574 [Note] InnoDB: Using CPU crc32 instructions
2024-08-07 14:00:41 23574 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2024-08-07 14:00:41 23574 [Note] InnoDB: Completed initialization of buffer pool
2024-08-07 14:00:41 23574 [Note] InnoDB: Highest supported file format is Barracuda.
2024-08-07 14:00:41 23574 [Note] InnoDB: 128 rollback segment(s) are active.
2024-08-07 14:00:41 23574 [Note] InnoDB: Waiting for purge to start
2024-08-07 14:00:41 23574 [Note] InnoDB: 5.6.50 started; log sequence number 1625977
2024-08-07 14:00:41 23574 [Note] RSA private key file not found: /app/mysql-5.6.50/data//private_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:41 23574 [Note] RSA public key file not found: /app/mysql-5.6.50/data//public_key.pem. Some authentication plugins will not work.
2024-08-07 14:00:41 23574 [Note] Binlog end
2024-08-07 14:00:41 23574 [Note] InnoDB: FTS optimize thread exiting.
2024-08-07 14:00:41 23574 [Note] InnoDB: Starting shutdown...
2024-08-07 14:00:43 23574 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/app/mysql-5.6.50/bin/mysqladmin -u root password 'new-password'
/app/mysql-5.6.50/bin/mysqladmin -u root -h db01 password 'new-password'

Alternatively you can run:

/app/mysql-5.6.50/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; /app/mysql-5.6.50/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /app/mysql-5.6.50/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

执行完毕后 ,过程中会有两个OK
[root@db01 ~]# echo $?
0

二进制安装启动失败解决

1
2
3
4
5
6
7
8
9
10
11
12
13
#启动mysql
[root@db02 mysql-5.6.50]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 244: my_print_defaults: command not found
/etc/init.d/mysqld: line 264: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

报错分析:
去/usr/local/mysql找my_print_defaults这个命令,但是发现找不到
#为啥要去这个目录去找?
源码安装时候我们指定了它的安装目录,但是我们二进制安装的MySQL,MySQL没有安装在官方默认的/usr/local/mysql目录下,我们使用二进制安装是官方指定了安装目录,所以找不到这个命令

#查看二进制安装的启动脚本
得知,官方指定默认安装位置是/usr/local

QQ_1723015813800

1
2
3
4
5
6
7
8
9
10
11
12
13
解决:
1.
# 报错原因:二进制安装的MySQL,MySQL没有安装在默认的/usr/local/mysql目录下
# 解决方案:
1) 方法一:修改脚本
:%s#/usr/local/mysql#/app/mysql-5.6.50#g

2)方法二:修改配置文件
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
basedir=/app/mysql-5.6.50
datadir=/app/mysql-5.6.50/data

QQ_1723016585520

1
2
3
[root@db02 mysql-5.6.50]#  /etc/init.d/mysqld start
Starting MySQL.Logging to '/app/mysql-5.6.50/data/db02.err'.
. SUCCESS!
1
2
3
4
#使用二进制安装的mysql的socket文件在哪?
/tmp
#为啥要在这?
官方也不知道你要在哪安装mysql,所有把socket文件放在tmp下,所有用户都有权限执行文件

连接mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#添加环境变量
echo 'export PATH="/app/mysql-5.6.50/bin:$PATH"' >/etc/profile.d/mysql.sh

#加载环境变量
source /etc/profile

msyql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 Source distribution
Copyright (c) 2000, 2020, 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>

源码安装MySQL5.7

下载 && 编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#1.下载5.7msyql源码
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.40.tar.gz

#2.解压并进入目录
tar xf mysql-boost-5.7.40.tar.gz
cd mysql-5.7.40

#Boost是一个十分强大的C++库,它包含了一系列为C++语言提升的扩展库,比如正则表达式、线程、文件系统操作等。编译器在编译依赖Boost库的C++代码时,需要找到相应版本的Boost库头文件和库文件

编译MySQL5.7以及更高的版本时,都需要下载并引用或者直接安装boost库,否则在执行cmake命令时会报如下错误:
-- Packaging as: mysql-5.7.40-Linux-x86_64
-- Looked for boost/version.hpp in and
-- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND
-- LOCAL_BOOST_DIR
-- LOCAL_BOOST_ZIP
-- Could not find (the correct version of) boost.
-- MySQL currently requires boost_1_59_0
CMake Error at cmake/boost.cmake:88 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

#解决
先下载Boost库,然后通过在cmake命令后面添加参数 -DWITH_BOOST=Boost库路径即可

#3.下载boosts库
mkdir /usr/local/boost
cd /usr/local/boost
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz


#4.安装依赖
yum -y install openssl-devel ncurses-devel autoconf libaio-devel cmake

#5.回到解压的源码目录并执行cmake
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql-5.7.40 \
-DMYSQL_DATADIR=/app/mysql-5.7.40/data \
-DMYSQL_UNIX_ADDR=/app/mysql-5.7.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0 \
-DWITH_BOOST=/usr/local/boost/boost_1_59_0.tar.gz

#查看是否cmake成功
[root@db03 mysql-5.7.40]# echo $?
0

#6.编译 && 安装
make && make install

初始化数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#1.拷贝启动脚本到init.d下
cp /app/mysql-5.7.40/support-files/mysql.server /etc/init.d/mysqld

# 2.创建MySQL程序用户
useradd mysql -s /sbin/nologin -M

#3.初始化数据库目录(注意保存初始密码)(注意要先设置环境变量)
echo 'export PATH="/app/mysql-5.7.40/bin:$PATH"' >/etc/profile.d/mysql57.sh
source /etc/profile

#初始化后带密码
mysqld --initialize --user=mysql --basedir=/app/mysql-5.7.40 --datadir=/app/mysql-5.7.40/data

#初始化后不带密码
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql-5.7.40 --datadir=/app/mysql-5.7.40/data

user:root
password:5?U1o9/PhQfl

启动MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#4.启动mysql
/etc/init.d/mysqld start
[root@db03 mysql-5.7.40]# /etc/init.d/mysqld start
Starting MySQL.2024-08-07T13:40:09.004006Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/db03.pid).

#解决报错
发现系统中有mariadb,日志文件不能写入
解决:删除mariadb

[root@db03 mysql-5.7.40]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@db03 mysql-5.7.40]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

#4.1再次启动mysql
/etc/init.d/mysqld start
Starting MySQL.Logging to '/app/mysql-5.7.40/data/db03.err'.
2024-08-07T13:44:36.244147Z mysqld_safe Directory '/app/mysql-5.7.40/tmp' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/app/mysql-5.7.40/data/db03.pid).

#同5.6一样,没有tmp目录无法写入socket文件
解决:
mkdir /app/mysql-5.7.40/tmp

授权:否则无法写入文件
chown -R mysql.mysql /app/mysql-5.7.40/

#再次启动服务(愉快启动了)
[root@db03 mysql-5.7.40]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/app/mysql-5.7.40/data/db03.err'.
SUCCESS!

连接数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 写入环境变量
echo 'export PATH="/app/mysql-5.7.40/bin:$PATH"' >/etc/profile.d/mysql57.sh

#重新加载环境变量
source /etc/profile

#连接数据库(
[root@db03 mysql-5.7.40]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

#使用给的密码登录
mysql -uroot -p'5?U1o9/PhQfl'

#重置密码
mysql> alter user root@localhost identified by '123';

二进制安装MySQL5.7

安装mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#1.下载5.7版本包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz

#2.创建安装目录
mkdir /app

#3.安装依赖
yum -y install openssl-devel ncurses-devel autoconf libaio-devel

#4.解压至/app目录
tar xf mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz -C /app

#5.改名
mv /app/mysql-5.7.40-linux-glibc2.12-x86_64/ /app/mysql-5.7.40

# 6.创建MySQL程序用户
useradd mysql -s /sbin/nologin -M

# 7.创建MySQL的配置文件到/etc/下(注意查看系统是否自带mariadb)
#查看系统是否有maroadb
[root@db04 mysql-5.7.40]# rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64

#卸载(如果有就卸载)
[root@db04 mysql-5.7.40]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

#编辑my.cnf
[root@db01 mysql-5.6.50]# vim /etc/my.cnf
[mysqld]

# 8.拷贝启动脚本
cp /app/mysql-5.7.40/support-files/mysql.server /etc/init.d/mysqld

#初始化数据库 (注意保存最后的密码)(先加环境变量)
echo 'export PATH="/app/mysql-5.7.40/bin:$PATH"' >/etc/profile.d/mysql57.sh
source /etc/profile

#初始化数据库目录
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql-5.7.40 --datadir=/app/mysql-5.7.40/data

[root@db04 mysql-5.7.40]# bin/mysqld --initialize --user=mysql --basedir=/app/mysql-5.7.40 --datadir=/app/mysql-5.7.40/data
2024-08-07T12:41:19.116519Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-07T12:41:19.599490Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-07T12:41:19.676071Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-07T12:41:19.731472Z 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: 58819e51-54ba-11ef-861d-000c290f3f79.
2024-08-07T12:41:19.732381Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-07T12:41:19.926326Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-07T12:41:19.926344Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-07T12:41:19.927081Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-07T12:41:20.019287Z 1 [Note] A temporary password is generated for root@localhost: a=G&ufhfb4=*
[root@db04 mysql-5.7.40]# echo $?
0

mysql
user:root
password:-jDYsiuZ1?y#

启动mysql

启动失败解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@db04 mysql-5.7.40]# service mysqld start
/etc/init.d/mysqld: line 239: my_print_defaults: command not found
/etc/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

#和5.6启动失败一样,默认去找/usr/local/mysql下找启动命令

解决:
1) 方法一:修改脚本
:%s#/usr/local/mysql#/app/mysql-5.7.40#g

2)方法二:修改配置文件(建议)
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
basedir=/app/mysql-5.7.40
datadir=/app/mysql-5.7.40/data

QQ_1723035262266

1
2
3
4
#再次启动
[root@db04 mysql-5.7.40]# service mysqld start
Starting MySQL.Logging to '/app/mysql-5.7.40/data/db04.err'.
. SUCCESS!

连接mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 写入环境变量
echo 'export PATH="/app/mysql-5.7.40/bin:$PATH"' >/etc/profile.d/mysql57.sh

#重新加载环境变量
source /etc/profile

#连接数据库(
[root@db03 mysql-5.7.40]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

#使用给的密码登录
mysql -uroot -p'a=G&ufhfb4=*'

#重置密码
mysql> alter user root@localhost identified by '123';

3.启动systemd管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@db03 ~]# vim /usr/lib/systemd/system/mysqld.service 
[Unit]
Description=MySQL Server
After=network.target
Wants=multi-user.target

[Service]
ExecStart=/app/mysql5.7.40/bin/mysqld_safe --defaults-file=/etc/my.cnf
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

#重新加载下
systemctl daemon-reload