0%

MySQL 5.7.22二进制安装详解

环境:CentOS 7 64位

本教程安装MySQL是通过编译过的二进制文件进行安装。是不针对特定平台的通用安装方法,使用的二进制文件是后缀为.tar.gz的压缩文件

下载

下载页面

1
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

卸载mariadb

1
yum -y remove mariadb-libs.x86_64

解压

解压缩到/usr/local/下面,mysql的主目录命名为mysql

1
tar zxf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

解压后移动到/usr/local/mysql目录

1
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

创建数据目录

在mysql下面创建数据库文件目录

1
2
mkdir -p /usr/local/mysql/{data,logs}
touch /usr/local/mysql/logs/error.log

创建用户

创建mysql的用户组和用户,并对mysql目录设置用户组和用户

1
2
3
groupadd --system mysql
useradd --system -g mysql -d /usr/lib/mysql -s /sbin/nologin -c "Mysql server" mysql
chown -R mysql.mysql /usr/local/mysql

配置my.cnf

配置简单的my.cnf

1
vi /etc/my.cnf
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
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock #套接字文件
default-character-set = utf8mb4
[mysqld]
port = 3306
user = mysql
pid-file = /usr/local/mysql/mysql.pid #pid文件
socket = /usr/local/mysql/mysql.sock #套接字文件,客户端需与服务端一致
basedir = /usr/local/mysql #Mysql安装路径
datadir = /usr/local/mysql/data #Mysql数据存放路径
log-error = /usr/local/mysql/logs/error.log #mysql生成的错误日志
tmpdir = /tmp
character_set_server = utf8mb4 #默认字符集
collation_server = utf8mb4_bin #默认排序规则
default-time-zone = '+08:00' #默认时区
default-storage-engine=INNODB #默认存储引擎INNODB
lower_case_table_names=1 #不区分大小写
log-bin = /usr/local/mysql/logs/mysql-bin #二进制日志,binlog用于记录用户对数据库操作的SQL语句信息
max_binlog_size = 100M #binlog每个日志文件大小
#master
server-id = 1
[mysql]
no-auto-rehash #命令补全
default-character-set = utf8mb4

初始化

初始化mysql并启动mysql服务

1
yum -y install libaio
1
2
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

在log-error中可以看到临时密码

1
grep 'temporary password' /usr/local/mysql/logs/error.log

创建RSA private key

1
./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

配置服务

配置服务,使用systemctl管理

1
vi /usr/lib/systemd/system/mysql.service
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
[Unit]
Description=MySQL Server
Documentation=man:mysqld(5.7)
Documentation=https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
Alias=mysql.service

[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/mysql.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=5
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysql.pid
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

启动

1
systemctl start mysql.service

配置环境变量

将MySQL命令添加环境变量

1
2
3
4
5
#set mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=${PATH}:${MYSQL_HOME}/bin

source /etc/profile

使用临时密码登陆并修改密码

1
mysql -uroot -p

修改MySQL的root密码,新密码在此为’root’

1
mysql> set password=password('root');

设定远程登录MySQL。在Linux下为了安全,默认是不允许MySQL本机以外的机器访问MySQL数据库服务,因此需要重新授权root。方便远程访问。

1
2
3
4
mysql> use mysql;
mysql> select Host,User from user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' identified by 'root';
mysql> flush privileges;

授权语句最后的‘root’是MySQL数据库root用户的新密码。

-------------    本文结束  感谢您的阅读    -------------
请作者一杯咖啡。