MySQL

centos7安装MySQL

源码安装mysql5.7

MySQL下载页
82eb6287bd56fe6055ec9edb35cdad1c.png

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
# 下载源码包
yum -y install wget
wget -O /opt/mysql-boost-5.7.44.tar.gz https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.44.tar.gz
tar -zxvf /opt/mysql-boost-5.7.44.tar.gz -C /opt/
cd /opt/mysql-5.7.44/
# 安装依赖
yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake
# 创建MySQL用户组和用户
useradd -r -s /sbin/nologin mysql
# 清空系统中的原有mariadb的配置文件
rm -rf /etc/my.cnf
#创建目录,并赋予权限 套接字目录、安装目录、日志目录
mkdir -p /var/run/mysql /usr/local/mysql /var/log/mysql
chown -R mysql:mysql /var/run/mysql /usr/local/mysql /var/log/mysql
#配置
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_BOOST=/opt/mysql-5.7.44/boost/boost_1_59_0/ \
.
# 编译 -j5 根据实际内核修改可加快编译速度
make -j5
make install

修改配置文件
`vim /etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/run/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

初始化数据库

1
2
3
cd /usr/local/mysql
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 初始化后会生成密码
1
2
3
4
5
6
7
8
9
10
11
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
ls support-files/
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on
systemctl status mysqld
systemctl start mysqld
bin/mysql -uroot -p 输入密码
set password=password('1');
exit
# password('密码')

添加环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/sbin' >> /etc/profile
source /etc/profile
mysql -uroot -p1
#开启远程连接MySQL
# 1为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1' WITH GRANT OPTION;
# 刷新
FLUSH PRIVILEGES;
#查看
select user,host from mysql.user;
exit
#安全设置
bin/mysql_secure_installation;
#输入密码后,前面两个分别为密码校验、不更改密码输入两次N,其余一直Y

yum安装mysql8.0版本

安装mysql 官方yum 源
yum install -y https://repo.mysql.com/mysql80-community-release-el7-6.noarch.rpm
yum 管理配置文件的工具
yum install -y yum-utils
打开 mysql8.0 的标签,关闭gpg验证 关闭 mysql5.7 的标签
vim /etc/yum.repos.d/mysql-community.repo
mysql.png
导入密钥

1
2
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
#设置 gpgcheck=0,则不需要导入

安装客户端和服务端
yum install -y mysql-community-client mysql-community-server
启动服务
systemctl start mysqld
修改临时密码

1
2
3
4
cat /var/log/mysqld.log
# u3:Kh2ig=?lgw为log日志中的密码
mysql -uroot -p'u3:Kh2ig=?lg'
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass2!';

测试
mysql -uroot -p'MyNewPass2!'


MySQL
http://example.com/2024/09/25/c7a2a78316fc4b499ffb55660130532e/
作者
John Doe
发布于
2024年9月25日
许可协议