首页 > 数据库 >源码、二进制安装MySQL5.7.39

源码、二进制安装MySQL5.7.39

时间:2022-11-29 13:45:27浏览次数:74  
标签:39 5.7 MySQL5.7 apps 源码 usr mysql local

源码、二进制安装MySQL5.7.39

1.源码安装

源码安装包下载链接https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39.tar.gz

1.1 安装依赖包

yum -y install wget cmake gcc gcc-c++ openssl-devel libaio-devel ncurses-devel libtirpc-devel 

1.2 下载包并解压

# 下载MySQL解压
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39.tar.gz
tar -xvf mysql-5.7.39.tar.gz -C /usr/local/src/

# 安装MySQL需要准备boost库
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -xvf boost_1_59_0.tar.gz -C /usr/local/src/

# 安装需要rpcsvc 下载解压后编译安装
wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz
tar -xvf rpc svc-proto-1.4.tar.gz -C /usr/local/src/
/usr/local/src/rpcsvc-proto-1.4/configure && make && make install

1.3 创建安装目录及用户

mkdir -p /apps/mysql-5.7.39/{data,tmp,binlog,logs}
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql

1.4 进行编译

cd /usr/local/src/mysql-5.7.39/
# 开始编译
cmake . -DCMAKE_INSTALL_PREFIX=/apps/mysql-5.7.39/ \
-DMYSQL_DATADIR=/apps/mysql-5.7.39/data/ \
-DMYSQL_UNIX_ADDR=/apps/mysql-5.7.39/tmp/mysql.sock \
-DWITH_BOOST=/usr/local/src/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_ZLIB=bundled \
-DWITH_LIBWRAP=0 \
-DWITH_SSL=system  	 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1  \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0

#如果编译出现错误,清除缓存重新编译
rm -rf CMakeCache.txt  CMakeFiles

# 进行编译
make -j2 && make install
MySQL编译配置选项

官方参考文档 https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html

-DCMAKE_INSTALL_PREFIX=/apps/mysql-5.7.39/		# MySQL安装目录安装位置
-DMYSQL_DATADIR=/apps/mysql-5.7.39/data/		# MySQL数据目录安装位置
-DMYSQL_UNIX_ADDR=/apps/mysql-5.7.39/tmp/mysql.sock		# mysql.sock路径 必须为绝对路径
-DWITH_BOOST=/usr/local/src/boost_1_59_0		# Boost存放位置
-DDEFAULT_CHARSET=utf8							# 默认字符集
-DDEFAULT_COLLATION=utf8_general_ci				# 排序规则
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii			# 启用额外的字符集类型
-DENABLED_LOCAL_INFILE=ON						# 启用本地数据导入支持
-DWITH_ZLIB=bundled								# zlib库支持方式 bundled为默认值  为捆绑库 system则为系统库
-DWITH_LIBWRAP=0 								# 是否包含libwrap(TCP 包装器)支持。
-DWITH_SSL=system  							    # SSL支持方式(yes|bundled|system)
# 启用禁用引擎支持 1 启用 0禁用
-DWITH_ARCHIVE_STORAGE_ENGINE=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
-DWITH_DEBUG=0									# 禁用debug 默认为禁用

# 另外记录
-DSYSCONFDIR=/etc/ 							    # my.cnf 文件目录
-DWITH_EMBEDDED_SERVER=1						# 编译嵌入式服务器支持 libmysql 已在 MySQL 8.0 中删除

1.5 my.cnf配置


# 编辑my.cnf配置文件
vim /etc/my.cnf
[client]
port = 3306
socket = /apps/mysql-5.7.39/tmp/mysql.sock
default-character-set = utf8

[mysqld]
port = 3306
user = mysql
basedir = /apps/mysql-5.7.39
datadir = /apps/mysql-5.7.39/data
pid-file = /apps/mysql-5.7.39/mysqld.pid
socket = /apps/mysql-5.7.39/tmp/mysql.sock
tmpdir = /apps/mysql-5.7.39/tmp
server-id=1
max_connections=100
max_connect_errors=10
character_set_server=utf8
log-bin = /apps/mysql-5.7.39/binlog/mysql-bin
log-error = /apps/mysql-5.7.39/logs/mysql.err
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

配置文件说明
vim /etc/my.cnf
[client]
port = 3306			# 链接端口号3306为默认值
socket = /apps/mysql-5.7.39/tmp/mysql.sock		# 用于本地连接的socket套接字
default-character-set = utf8					# 默认编码方式

[mysqld]
port = 3306										# 服务端口号。默认3306
user = mysql									# MySQL启动用户
basedir = /apps/mysql-5.7.39					# MySQL安装目录
datadir = /apps/mysql-5.7.39/data				# MySQL数据库存放目录
pid-file = /apps/mysql-5.7.39/mysqld.pid		# pid文件目录
socket = /apps/mysql-5.7.39/tmp/mysql.sock		# 为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
tmpdir = /apps/mysql-5.7.39/tmp					# MySQL存放临时文件的目录
server-id = 1									# MySQL服务的唯一编号,每个mysql服务Id需唯一
max_connections = 100							# 最大连接数
max_connect_errors = 10							# 最大错误连接数
character_set_server = utf8						# 数据库默认字符集,主流字符集支持一些特殊表情符号(表情符占4字节)
log-bin = /apps/mysql-5.7.39/binlog/mysql-bin	# 开启二进制日志功能,binlog数据位置
log-error = /apps/mysql-5.7.39/logs/mysql.err # mysql生成的错误日志存放的路径

# sql_mode,定义了mysql应该支持的sql语法,数据校验
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
# NO_ENGINE_SUBSTITUTION:如果需要的存储引擎被禁用或未编译,可以防止自动替换存储引擎
# STRICT_TRANS_TABLES:存储引擎启用严格模式,非法数据值被拒绝
# NO_AUTO_CREATE_USER:禁止GRANT创建密码为空的用户
# NO_AUTO_VALUE_ON_ZERO:默认情况下自增长列是从1开始的,如果你插入值为0的数据会报错,设置这个之后就可以正常插入为0的数据了
# NO_ZERO_IN_DATE:在严格模式,不接受月或日部分为0的日期
# NO_ZERO_DATE:在严格模式,不要将 ‘0000-00-00ʹ做为合法日期
# ERROR_FOR_DIVISION_BY_ZERO:在严格模式,在INSERT或UPDATE过程中,如果被零除(或MOD(X,0)),则产生错误(否则为警告)。如果未给出该模式,被零除时MySQL返回NULL
# PIPES_AS_CONCAT:将||视为字符串连接操作符(+)(同CONCAT()),而不视为OR
# ANSI_QUOTES:将‘”'视为识别符引号(‘`'引号字符),不要视为字符串的引号字符

1.6 初始化数据库

chown -R mysql:mysql /apps/mysql-5.7.39/
# 初始化mysql目录,如果失败请删除data下所有文件重来 rm -rf /apps/mysql-5.7.39/data/*
# --initialize-insecure	  # 不生成初始密码
# --initialize			  # 生成初始密码
/apps/mysql-5.7.39/bin/mysqld --initialize-insecure --user=mysql --basedir=/apps/mysql-5.7.39 --datadir=/apps/mysql-5.7.39/data

# 开启ssl加密连接
/apps/mysql-5.7.39/bin/mysql_ssl_rsa_setup --initialize-insecure --user=mysql --basedir=/apps/mysql-5.7.39 --datadir=/apps/mysql-5.7.39/data
chown mysql:mysql /etc/my.cnf
chmod +r /apps/mysql-5.7.39/data/server-key.pem

1.7 环境变量

# 设置环境变量
echo 'PATH=/apps/mysql-5.7.39/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh


# 加入systemd管理
vim /usr/lib/systemd/system/mysqld.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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=/apps/mysql-5.7.39/bin/mysqld --defaults-file=/etc/my.cnf

[root@node1 mysql]# systemctl daemon-reload			# 刷新配置
[root@node1 mysql]# systemctl enable mysqld.service # 设置开机启动
[root@node1 mysql]# systemctl start mysqld.service	# 启动


# 设置server,可以不做
cp /apps/mysql-5.7.39/support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld 	#增加mysql服务
chkconfig mysqld on		#开机启动
mysqld start			#启动服务

1.8 数据库登录

# 如果前面初始化数据库使用--initialize变量时会生成密码,需要在日志文件中查看
[root@node1 ~]# cat /apps/mysql-5.7.39/logs/mysql_5_7_39.err |  grep localhost
2022-11-28T04:22:45.489946Z 1 [Note] A temporary password is generated for root@localhost: jRJj<cz?;21I
[root@node1 ~]# mysql -uroot -p'jRJj<cz?;21I'

# 如果设置的为--initialize-insecure变量则直接登录就行
[root@node1 ~]# mysql -uroot -p
Enter password: 

# 测试无碍
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 如果想要更换密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

1.9 安全初始化

[root@node1 mysql-5.7.39]# mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
======================================= 是否提高密码强度 ==========================================
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2				# 选择强度等级
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 

==================================   是否继续使用提供的密码  ==========================
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
==================================      是否删除匿名用户      ==========================
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
==================================      是否关闭root远程登录      ==========================
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.
==================================      是否删除测试数据库      ==========================
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
==================================      是否重新加载         ==========================
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 

2. 二进制安装

下载链接https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz

2.1 下载并解压

# 下载二进制文件
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
# 解压到/usr/local文件夹
tar -xvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# 将解压的文件创建软连接,以下操作用软连接不用真实路径
ln -s /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql

2.2 创建目录及用户

mkdir -p /usr/local/mysql/{data,tmp,binlog,logs}
groupadd -r -g 306 mysql
useradd -s /sbin/nologin -M -g 306 -u 306 mysql
# 赋权
chown mysql:mysql /usr/local/mysql/

2.3 初始化数据库

# 设置环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

2.4 my.cnf配置

vim /etc/my.conf
[client]
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock
default-character-set = utf8mb4

[mysqld]
datadir=/usr/local/mysql/data/
socket = /usr/local/mysql/tmp/mysql.sock
basedir = /usr/local/mysql
pid-file = /usr/local/mysql/mysqld.pid
innodb_buffer_pool_size=128M
port=3306
symbolic-links=0

[mysqld_safe]
log-bin = mysql-bin
log-error=/usr/local/mysql/log/mysql.err

2.5 配置环境,进行开机

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on		#设置开机启动
systemctl start mysqld	or service mysqld start

2.6 登录及初始化

mysql -uroot -p'J1B0)mb;EIbw'
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 为了安全还是做好初始化比较好
mysql_secure_installation

报错集

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 修改密码解决 
mysql> ALTER USER USER() IDENTIFIED BY 'F1asjdfkl42345';

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

mysql> ALTER USER USER() IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
# 密码强度问题
mysql> set global validate_password_policy=LOW;	#密码强度
mysql> set global validate_password_length=9;	#密码长度
mysql> FLUSH PRIVILEGES; 

Package ‘libtirpc‘, required by ‘virtual:world‘, not found

-- Checking for module 'libtirpc'
--   Package 'libtirpc', required by 'virtual:world', not found
CMake Error at cmake/rpc.cmake:76 (MESSAGE):
  Could not find rpc/rpc.h in /usr/include or /usr/include/tirpc
Call Stack (most recent call first):
  rapid/plugin/group_replication/configure.cmake:60 (MYSQL_CHECK_RPC)
  rapid/plugin/group_replication/CMakeLists.txt:25 (INCLUDE)
  
[root@node1 mysql-5.7.39]# yum -y install libtirpc-devel

Could not find rpcgen:

-- Performing Test X_PUTLONG_NOT_USE_CONST - Failed
CMake Error at rapid/plugin/group_replication/rpcgen.cmake:100 (MESSAGE):
  Could not find rpcgen
Call Stack (most recent call first):
  rapid/plugin/group_replication/CMakeLists.txt:36 (INCLUDE)
  
wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz
[root@node1 ~]# tar -xvf rpc svc-proto-1.4.tar.gz -C /usr/local/src/
[root@node1 ~]# /usr/local/src/rpcsvc-proto-1.4/configure && make && make install 

参考文档

https://blog.332b.com/?p=1575

标签:39,5.7,MySQL5.7,apps,源码,usr,mysql,local
From: https://www.cnblogs.com/feifa/p/16935176.html

相关文章