检查mysql环境是否已存在
虽然我的是纯净系统,但别人的不能保证,为了避免发生什么问题我们还是先检查下mysql是否已经安装过
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
我这里要卸载mariadb
[root@localhost ~]# yum remove mariadb-libs-5.5.64-1.el7.x86_64
假如你发现类似的就和我一样删除就好了
切换阿里云镜像源
先安装wget
[root@localhost ~]# yum install wget -y
然后下载阿里云yum源配置
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
然后生成缓存
[root@localhost ~]# yum makecache
显视元数据缓存已建立就代表完成了
然后我们更新一下yum
[root@localhost ~]# yum update -y
这可能需要一点时间,耐心等待一下
下载国内的mysql rpm包并安装
这里选择腾讯云的镜像,也可以用阿里云的镜像、华为云的镜像
https://downloads.mysql.com/archives/community/
地址为https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-5.7/
我这里直接通过下载地址下载
先是server包
[root@localhost ~]# wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-5.7/mysql-community-server-5.7.31-1.el7.x86_64.rpm
然后是client包
[root@localhost ~]#wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-5.7/mysql-community-client-5.7.31-1.el7.x86_64.rpm
还有common
[root@localhost ~]# wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-5.7/mysql-community-common-5.7.31-1.el7.x86_64.rpm
最后还有一个lib
[root@localhost ~]# wget https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-5.7/mysql-community-libs-5.7.31-1.el7.x86_64.rpm
在此之前我们还要先安装三个依赖环境
net-tools.x86_64,libaio.x86_64,perl.x86_64
我们直接使用yum安装
- `[root@localhost ~]# yum install -y perl.x86_64
`
- `[root@localhost ~]# yum install -y libaio.x86_64
`
- `[root@localhost ~]# yum install -y net-tools.x86_64``
`
然后按照顺序安装mysql的依赖
[root@localhost ~]# rpm -ivh mysql-community-common-5.7.31-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-libs-5.7.31-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-client-5.7.31-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm
然后我们重启下mysql服务
[root@localhost ~]# service mysqld.service restart
我们查看下默认密码
[root@localhost etc]# grep ‘temporary password’ /var/log/mysqld.log
2021-03-20T09:49:32.224110Z 1 [Note] A temporary password is generated for root@localhost: QA#$Hlql0@
我的密码为 QA#$Hlql0@
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.25
Copyright (c) 2000, 2019, 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>
登录成功,接着改下密码和远程连接
修改密码并开启远程连接
修改密码
这里因为mysql的新版本限制了密码复杂度,所以我们需要设置一个稍微复杂的密码
mysql> set password=password(‘这里输入你想改的密码’);
Query OK, 0 rows affected, 1 warning (0.00 sec)
密码需要包含数字和特殊符号,以及大写字母和小写字母
当然你设置完成以后可以就将密码限制关闭后在重新改密码,这里不过多演示
开启远程连接
打开mysql数据库
mysql> use mysql;
修改一条数据使其支持远程连接
mysql> update user set Host = ‘%’ where Host = ‘localhost’ and User=’root’;
刷新系统权限相关表
mysql> flush privileges;
然后用Navicat或其他工具连接mysql服务测一下(在此之前请确保防火墙开放了3306端口)
配置Mysql
默认情况下mysql安装完后默认的配置文件是/etc/my.cnf
配置文件修改
[root@localhost ~]# vim /etc/my.cnf
改为如下配置
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10% adjust value here, 50%-70% of total RAM)
innodb-buffer-pool-size = 4G
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
server-id=1
# Row format required for
binlog-format=row
# Directory must exist. This path works for Linux. Other OS may require
# different path.
log-bin=/home/logs/mysql-bin
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join-buffer-size = 256M
sort-buffer-size = 128M
read-rnd-buffer-size = 128M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1 # may change to 2 or 0
innodb-flush-method = O_DIRECT
重启mysql
[root@localhost ~]# service mysqld.service restart
重启mysql后可能出现下面异常
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code. See “systemctl status mysqld.service” and “journalctl -xe” for details.
查看mysql启动日志
[root@localhost ~]# tail -n 20 /var/log/mysqld.log
2021-04-13T09:01:07.890615Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.31-log) starting as process 102891 …
mysqld: File ‘/var/log/mysql/mysql-bin.index’ not found (Errcode: 13 - Permission denied)
2021-04-13T09:01:07.895412Z 0 [ERROR] Aborting
2021-04-13T09:01:07.895466Z 0 [Note] Binlog end
2021-04-13T09:01:07.895616Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2021-04-13T09:01:09.151790Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-13T09:01:09.154849Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.31-log) starting as process 102919 …
mysqld: File ‘/var/log/mysql/mysql-bin.index’ not found (Errcode: 13 - Permission denied)
2021-04-13T09:01:09.191759Z 0 [ERROR] Aborting
2021-04-13T09:01:09.191828Z 0 [Note] Binlog end
2021-04-13T09:01:09.192011Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
2021-04-13T09:01:10.610024Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-13T09:01:10.611619Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.31-log) starting as process 102949 …
mysqld: File ‘/var/log/mysql/mysql-bin.index’ not found (Errcode: 13 - Permission denied)
2021-04-13T09:01:10.614052Z 0 [ERROR] Aborting
2021-04-13T09:01:10.614097Z 0 [Note] Binlog end
2021-04-13T09:01:10.614195Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
表明mysql无/var/log/mysql目录权限,采用下面命令修改权限
[root@localhost ~]# chown -R mysql:mysql /var/log/mysql
再次重启mysql服务
binlog配置问题
mkdir -p /home/logs
chown -R mysql.mysql
vi /etc/my.cnf
在[mysqld]分组中加入
server-id=777777
log-bin=/home/logs/mysql-bin
binlog-format=ROW
之后的binlog文件将/home/logs目录中且文件名为mysql-bin.000001, mysql-bin.000002, …
my.cnf配置文件注意-与的区别,配置文件一般是-,而环境变量一般是