CentOS7下安装mysql-5.7.24

一:安装前期准备

1、检查是否已经安装过mysql

[root@localhost /]# rpm -qa | grep mysql

图1
如果已经安装的有的化,需要先把之前的删除了

rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64

因为,我这个是新装的虚拟机,因为是空的。删除之后记得查看。
图2

2、查询所有mysql对应的文件夹

[root@hadoop-master ~]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[root@hadoop-master ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/share/mysql
[root@hadoop-master ~]# 

删除上面的目录或者文件

[root@hadoop-master ~]# rm -rf /usr/lib64/mysql /usr/share/mysql /etc/selinux/targeted/active/modules/100/mysql

删除完了之后再检查一下看看还有没有

[root@hadoop-master ~]# whereis mysql
mysql:[root@hadoop-master ~]# find / -name mysql
[root@hadoop-master ~]# 

3、下载linux版本的mysql安装包

打开目录

[root@hadoop-master local]# cd /usr/local

下载安装包到此目录

[root@localhost /]#  wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

当然也可以去官网下载:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

二:安装mysql

1、解压文件

[root@hadoop-master local]# tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

图2

2、将解压后的文件重新命名

[root@hadoop-master local]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql

3、在/usr/local/mysql目录下创建data目录

[root@hadoop-master local]# mkdir /usr/local/mysql/data

图三

4、编译安装并初始化mysql

特别注意日志输出的末尾处是mysql的初始化密码,一定要记住

[root@hadoop-master bin]# cd /usr/local/mysql/bin
[root@hadoop-master bin]# ./mysqld --initialize --user=root --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

编译输出日志:
日志

我这里的mysql的root账户初始化密码就是:_H5X-*gQI_hD

5、配置文件my.cnf

[root@hadoop-master bin]# vi /etc/my.cnf

[mysqld]
port=3306
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
max_connections=151
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 设置忽略大小写
lower_case_table_names = 1
# 指定编码
character-set-server=utf8
collation-server=utf8_general_ci
# 开启ip绑定
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8

6、启动mysql

[root@hadoop-master bin]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.2020-08-02T15:58:23.384616Z 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 (/usr/local/mysql/data/hadoop-master.pid).

发现报错了,根据提示我们需要把写权限Create writable for user ‘mysql’.
创建myslq组和mysql用户

[root@hadoop-master data]# groupadd mysql
[root@hadoop-master data]# cat /etc/passwd |grep mysql
[root@hadoop-master data]# useradd -r -g mysql mysql
[root@hadoop-master data]# cat /etc/group | grep mysql
mysql:x:1001:
[root@hadoop-master data]# cat /etc/passwd |grep mysql
mysql:x:987:1001::/home/mysql:/bin/bash

更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限

root@hadoop-master data]# chown -R mysql:mysql /usr/local/mysql
[root@hadoop-master data]# chmod -R 755 /usr/local/mysql

本人走弯路了,其实上面的步骤可以提前,如果遇到下面的异常

[root@hadoop-master bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2020-08-02T16:32:14.093255Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-02T16:32:14.094353Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-08-02T16:32:14.094373Z 0 [ERROR] Aborting

可以直接清空/usr/local/mysql/data/ 目录下的所有内容,然后继续往下走

[root@hadoop-master mysql]# cd /usr/local/mysql/bin
[root@hadoop-master bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2020-08-02T16:35:03.336722Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-02T16:35:03.440657Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-02T16:35:03.470791Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-02T16:35:03.528707Z 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: 1dc1c1cb-d4de-11ea-9c65-000c29f03fc4.
2020-08-02T16:35:03.529393Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-02T16:35:03.530486Z 1 [Note] A temporary password is generated for root@localhost: =4tlBRlrbObB

记住这个初始化密码:=4tlBRlrbObB

继续启动mysql

[root@hadoop-master bin]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL. SUCCESS! 

看到Starting MySQL. SUCCESS代表启动服务成功了

7、添加软连接,并重启动mysql

添加软连接主要是为了更加方便使用mysql命令

[root@hadoop-master bin]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@hadoop-master bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@hadoop-master bin]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

8、登录mysql

使用的初始密码就是上面的:=4tlBRlrbObB

[root@hadoop-master bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24

Copyright (c) 2000, 2018, 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> set password for root@localhost = password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

我们这里使用了密码重置,将数据库root账户密码重置:

set password for root@localhost = password('root')

9、开放远程连接

如果不开放,只能自己机器里玩了,外部的连不上的。

mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;

设置远程连接

10、开机自启

1、将服务文件拷贝到init.d下,并重命名为mysql
[root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2、赋予可执行权限
[root@localhost /]# chmod +x /etc/init.d/mysqld
3、添加服务
[root@localhost /]# chkconfig --add mysqld
4、显示服务列表
[root@localhost /]# chkconfig --list

显示结果:

[root@hadoop-master bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hadoop-master bin]# chmod +x /etc/init.d/mysqld
[root@hadoop-master bin]# chkconfig --add mysqld
[root@hadoop-master bin]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

11、外部工具连接mysql

连接成功
如果不成功,大概率就是防火墙没开放3306端口,根据如下指令,将端口添加

[root@hadoop-master bin]# iptables -A INPUT -m state --state NEW -m udp -p udp --dport 3306 -j ACCEPT
[root@hadoop-master bin]# service iptables save
[root@hadoop-master bin]# service iptables restart
[root@hadoop-master bin]# lsof -i tcp:3306

如果没有iptables命令,使用如下命令安装即可

[root@hadoop-master bin]# yum install iptables-services

至此,安装结束了。如果对你有帮助,请点赞支持下,谢谢。

猜你喜欢

转载自blog.csdn.net/u011047968/article/details/107749423