CentOS7.5安装MySQL5.7.24

 


 

卸载mariaDB

centos7.5默认安装安装了mariaDB

 

[root@localhost mysql]# rpm -qa|grep mariadb
[root@localhost mysql]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64
卸载mariadb,如果不能卸载,则增加rpm强制卸载参数 --nodeps
[root@localhost mysql]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

 

下载

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

 

#下载到/usr/local/src
[root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local
[root@localhost local]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
[root@localhost local]# cd /usr/local/mysql

安装

创建用户和用户组

[root@localhost mysql]# groupadd mysql
// -r, --system             create a system account
// -s, --shell SHELL login  shell of the new account
[root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql
#创建数据库存储数据目录,自定义
[root@localhost mysql]# mkdir -p /db/mysql
[root@localhost mysql]# chown -R mysql:mysql /db/mysql

 

配置文件

#编辑my.cnf文件
[root@localhost mysql]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /db/mysql
port = 3306
socket = /tmp/mysql.sock
explicit_defaults_for_timestamp=true

#优化
max_connections = 1000
back_log = 1024
innodb_log_file_size = 64M
#256M
innodb_buffer_pool_size=268435456
slow_query_log=1
long_query_time=1

 

添加环境变量

##编辑etc目录下的profile文件
[root@localhost mysql]# vi /etc/profile

#在profile文件最后添加
export PATH=$PATH:/usr/local/mysql/bin

或者多个

export PATH=$PATH:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/mysql/lib:/usr/local/openssl/bin:/usr/local/php/bin:/usr/local/mongodb/bin

#重新加载etc目录下的profile文件
[root@localhost mysql]# source /etc/profile

 

初始化

[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/  --datadir=/db/mysql

 

错误: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
yum -y install libaio

 

要记住刚才生成的随机密码
[Note] A temporary password is generated for root@localhost: uejd873ltl

 

复制启动脚本

 

[root@localhost mysql]# vim support-files/mysql.server 
                        basedir=/usr/local/mysql
                        datadir=/db/mysql
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

 

启动mysql

 

[root@localhost mysql]# service mysqld start
或者
[root@localhost mysql]# /etc/init.d/mysqld start

 

可能会出现的错误

 

一般不会出现什么问题
ERROR! The server quit without updating PID file (/db/mysql/localhost.localdomain.pid).

 

1、关闭selinux
vim /etc/selinux/config 将SELINUX设置为disabled
SELINUX=disabled

 

查看mysql状态

 

[root@localhost mysql]# /etc/init.d/mysqld status

 

登录mysql修改密码

 

[root@localhost mysql]# mysql -uroot –p
输入临时密码 ##mysql生成的临时密码,如:uejd873ltl
mysql> set password=password('timophp'); ##修改密码
#刷新权限
mysql> flush privileges;

 

如果忘记了刚才生成的密码,请看 http://www.timophp.dev/blog/show/10003/

 

设置

开机自启

[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --level 35 mysqld on
# 查看开机自启动
[root@localhost mysql]# chkconfig --list

 

内网访问限制

允许192.168.1.250这台主机可以访问mysql

 

[root@timophp.com ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.250" port protocol="tcp" port="3306" accept"
[root@timophp.com ~]# firewall-cmd --reload

 

可以关闭firewalld(本地测试或阿里云专有网络)

 

[root@localhost mysql]# systemctl stop firewalld