CentOS7二进制安装MySQL5.7

首先检查是否有默认的数据库:
rpm -qa | grep mysql
如果有,使用一下命令卸载
rpm -e 已经存在的MySQL全名


卸载CentOS7系统自带mariadb:
#查看系统自带的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.35-3.el7.x86_64
# 卸载系统自带的Mariadb
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64
# 删除etc目录下的my.cnf ,一定要删掉,等下再重新建,之前我将就用这个文件,后面改配置各种不生效
[root@localhost ~]# rm /etc/my.cnf

创建用于运行MySQL的组和普通用户(非操作系统用户):
groupadd mysql
useradd -g mysql -s /sbin/nologin -M mysql

下载安装,从官网安装下载,我下载的位置在/usr/local/src
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
tar -xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz 
mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql

更改mysql 目录下所有文件夹所属的用户组和用户,以及权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql

创建mysql数据目录并授权
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql/data

创建MySQL主配置文件:
vim /etc/my.cnf

[client]
port = 3306
default-character-set=utf8

[mysqld]
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION


然后切换到mysql的bin目录执行下面命令初始化数据库:
[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2020-12-28T03:10:33.852936Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-28T03:10:34.058908Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-28T03:10:34.108848Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-28T03:10:34.182906Z 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: 401f18fc-48ba-11eb-9500-000c2900944c.
2020-12-28T03:10:34.184112Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-28T03:10:34.676065Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-28T03:10:34.740466Z 1 [Note] A temporary password is generated for root@localhost: QvCIn*y?6f10

#注意最后面的密码要记住:QvCIn*y?6f10


配置系统环境变量:
vim /etc/profile
#在文件的最后新起一行,插入:
export PATH=$PATH:/usr/local/mysql/bin

source /etc/profile

临时启动测试(如下表示已正常启动):
[root@localhost bin]# ./mysqld_safe 
2020-12-10T07:27:11.165654Z mysqld_safe Logging to '/usr/local/mysql/data/mysqldb.localhost.err'.
2020-12-10T07:27:11.200006Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
...

pkill mysql

重新打开一个终端杀掉进程再使用MySQL自带启动脚本启动测试(如下表示已正常启动):
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS! 
[root@localhost support-files]# ./mysql.server stop
设置MySQL使用systemctl服务:
[root@localhost mysql]# vim /usr/lib/systemd/system/mysqld.service

[Unit]

Description=MySQL Server
After=network.target
After=syslog.target

[Service]

User=mysql
Group=mysql
Type=forking

PermissionsStartOnly=true

ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/usr/local/mysql/support-files/mysql.server restart

LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target

启动和关闭测试:
systemctl daemon-reload
systemctl start mysqld.service 
systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-12-27 22:20:43 EST; 17s ago
  Process: 2337 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
 Main PID: 2348 (mysqld_safe)
   CGroup: /system.slice/mysqld.service
           ├─2348 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
           └─2516 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=localhost....

Dec 27 22:20:42 localhost.localdomain systemd[1]: Starting MySQL Server...
Dec 27 22:20:43 localhost.localdomain systemd[1]: Started MySQL Server.

添加开机自己服务:
systemctl enable mysqld.service

查看端口并登录:
[root@localhost ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      926/sshd: /usr/sbin 
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1059/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2764/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      926/sshd: /usr/sbin 
tcp6       0      0 ::1:25                  :::*                    LISTEN      1059/master         
#下面输入初始话时记录的密码(修改密码并创建远程登录账户):
[root@localhost ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32

Copyright (c) 2000, 2020, 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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

关闭防火墙和selinux:
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

#最后使用桌面版数据库连接工具测试连接成功:Navicat Premium

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虔旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值