二进制安装Mysql5.7.31

Mysql下载地址:https://downloads.mysql.com/archives/community/ ,我们今天安装的是5.7.31,Linux-Generic ,64-bit 版本。

基础环境

  • 关闭防火墙和selinux

  • 安装基础依赖包

1
yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl libaio wget lrzsz vim libnuma* bzip2 xz
  • 新建mysql用户
1
2
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
  • 规划mysql数据目录
1
mkdir /opt/mysql

安装Mysql

下载Mysql

1
2
3
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

初始化mysql

1
2
3
4
5
6
7
8
9
10
cd /usr/local/mysql/bin/
./mysqld --initialize --user=mysql --datadir=/opt/mysql
2021-12-31T03:06:17.837238Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-31T03:06:17.837469Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2021-12-31T03:06:26.994083Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-31T03:06:28.532406Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-31T03:06:28.745765Z 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: a5d85ca0-69e6-11ec-8e9c-525400ef6113.
2021-12-31T03:06:28.768840Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-31T03:06:29.738532Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-31T03:06:29.867148Z 1 [Note] A temporary password is generated for root@localhost: rkYWw1.(vp_q #注意这里是root密码

编辑Mysql配置文件/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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[mysqld]
datadir=/opt/mysql #修改mysql数据目录
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
max_connections=1000
max_connect_errors=100
character-set-server=utf8mb4
default-storage-engine=INNODB
tmp_table_size = 64M
max_heap_table_size = 64M
max_allowed_packet = 256M
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysql]
default-character-set=utf8mb4
auto-rehash
[client]
port=3306
default-character-set=utf8mb4
[mysqld_safe]
log-error=/var/log/mysql/mysql.log #修改日志路径
pid-file=/var/run/mysql/mysql.pid #修改pid路径
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

新建Mysql所需目录

1
2
3
4
5
6
7
8
# 新建日志目录
mkdir /var/log/mysql
touch /var/log/mysql/mysql.log
chown -R mysql.mysql /var/log/mysql/
#新建sock目录
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql/
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

配置环境变量

1
2
echo 'PATH=.:$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile

配置Mysql启动文件

1
2
3
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改/etc/init.d/mysqld 文件中数据目录
datadir=/opt/mysql

chkconfig --add mysqld

chkconfig --list

systemctl start mysqld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@mysqldb-144 mysql]# ss -tnl |grep 3306
LISTEN 0 128 [::]:3306 [::]:*
[root@mysqldb-144 mysql]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Fri 2021-12-31 11:21:51 CST; 27min ago
Docs: man:systemd-sysv-generator(8)
Process: 8155 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 8165 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─8173 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysql --pid-file=/opt/mysql/mysqldb-144.pid
└─8409 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysql --plugin-dir=/usr/local/mysql/lib/plug...
Dec 31 11:21:50 mysqldb-144 systemd[1]: Starting LSB: start and stop MySQL...
Dec 31 11:21:51 mysqldb-144 mysqld[8165]: Starting MySQL. SUCCESS!
Dec 31 11:21:51 mysqldb-144 systemd[1]: Started LSB: start and stop MySQL.

修改密码

方法1>

登录mysql,进行修改

1
mysql> alter user 'root'@'localhost' identified by 'passwd';

方法2>

使用mysqladmin工具修改

1
2
mysqladmin -u root -p password new_pass #newpass是要设置的新密码
Enter password: #这里是输入之前的密码(初始化生成的)

方法3>

mysql8.0以上版本使用mysql_native_password进行密码验证

1
mysql> ALTER USER 'root'@'localhost'IDENTIFIED WITH mysql_native_password BY 'new_pass';