MySql5.7.35在CentOs 7.7_64位上安装部署、初始化和创建实例
环境信息:
centos 7.7 64位
mysql 5.7.35
终端工具 xshell https://www.netsarang.com/zh/xshell-download/
传输工具 XFTP
https://www.netsarang.com/zh/xftp-download/
这两个工具对个人用户是免费的 不需要必须破解
写在前面
最近心血来潮在腾讯云搞了台服务器,在装数据库时可把我给难受坏了,从18点搞到凌晨1点终于搞好了。本以为半个小时就装好的事搞了7个小时,可能是我把Linux想的太简单了,以为敲几个命令行,就能把事解决。
1.下载
我是在windows上下载好.tar安装包通过ftp传输工具上传上服务器的,
官网下载链接:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
我选用的是通用版的
通过XFTP工具传输到 centos 服务器的 /usr/local 目录下,
为什么要放到这个文件夹下呢?因为MySQL的配置文件里的配置默认就是这个位置,如果不安这个路径安装,会在服务器启动的时候因为找不到相关文件带来不必要的麻烦,我刚开始也是不知道,MySQL服务器总是找不到文件,我也不是干这一行的,也不知道怎么弄,在网上搜了一堆,然并卵,最后只能卸载了重新装。
2.安装
进入文件夹内解压缩:
[root@VM-0-2-centos local]# cd /usr/local/
[root@VM-0-2-centos local]# tar -xvf mysql-5.7.35-linux-glibc2.12-x86_64.tar
解压出来会有两个压缩包:一个test版的,一个标准版的,再解压标准版的
[root@VM-0-2-centos local]# tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
配置MySQL环境
[root@VM-0-2-centos local]# vim /etc/profile
按 i 进行插入,将下面的代码 粘贴在文件的最下方后 按 Esc 输入 :wq 进行保存
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
export MYSQL_HOME="/usr/local/mysql"
export ms="/usr/local/mysql"
修改mysql默认配置
[root@VM-0-2-centos local]# vi /etc/my.cnf
在 [mysqld] 下方添加如下内容:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
在 [client] 下方添加如下内容:如果没有[client] 就将下面的全复制粘贴在文件里,有的话只需要粘贴后两行
[client]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
default-character-set=utf8 是修改数据库 默认的字符集为utf8
socket=/var/lib/mysql/mysql.sock 设置socket路径,不设置,有可能会报下面的错误
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
错误产生原因解析
根据配置文件“/etc/my.cnf”提供的路径,未找到相应的socket文件,就会引发此错误,所以我们要手动设置socket路径
我的整体配置如下
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
datadir=/var/lib/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
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[client]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
这里说一下,我的最初并没有创建 **[mysqld_safe]**下的文件,在最后启动时报了个错误,大概意思就是 没找到文件 也没有写权限
Starting MySQL.2021-09-17T15:26:23.032970Z 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 (/var/lib/mysql/VM-0-2-centos.pid).
于是我手动创建了[mysqld_safe]下的指定文件,并给mysql权限
[root@VM-0-2-centos local]# mkdir /var/log/mariadb
[root@VM-0-2-centos local]# touch /var/log/mariadb/mariadb.log
[root@VM-0-2-centos local]# chown -R mysql:mysql /var/log/mariadb/
创建用户
[root@VM-0-2-centos local]# groupadd mysql
[root@VM-0-2-centos local]# useradd mysql -g mysql
[root@VM-0-2-centos local]# chown mysql:mysql /usr/local/mysql -R
MySQL下载依赖包
不安装依赖,在初始化数据库时会报以下错误
[root@VM-0-2-centos mysql]# bin/mysqld --initialize --user=mysql
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
之前我在VMware上玩的时候是不用下载依赖的,可能是因为系统版本不同吧。
先查看是否安装有 libaio 有的话就不用下载了
[root@VM-0-2-centos mysql]# yum search libaio
没有就下载一下
[root@VM-0-2-centos mysql]# yum install -y libaio
[root@VM-0-2-centos mysql]# yum -y install numactl
初始化MySQL,只有第一次安装时需要初始化
[root@VM-0-2-centos mysql]# bin/mysqld --initialize --user=mysql
此时会输出一个初始密码记得保存,
留意下面这一行 后面生成的 +iUe<!-ky69x 为数据库随机生成的初始密码,
网上有人说生成的密码是临时性的,会失效,所以登录上要先改密码
2021-09-17T15:01:33.356837Z 1 [Note] A temporary password is generated for root@localhost: +iUe<!-ky69x
启动数据库
在 usr/local/mysql 目录下启动,启动成功会出现
Starting MySQL. SUCCESS! 字样
[root@VM-0-2-centos mysql]# service mysql start
Starting MySQL. SUCCESS!
连接数据库
[root@VM-0-2-centos ~]# mysql -u root -p
敲回车后会让你输入密码
注意:输入密码时 窗口不显示字符 不要以为你没输入上
密码正确。连接成功后会变成箭头 >
修改数据库密码
有很多方式可以修改密码,这只是一种
mysql> set password=password('这里输入你要设的密码');
授予远程连接权限
//授权 只需要把密码改了 其他不用动
mysql>grant all privileges on *.* to 'root' @'%' identified by '你的密码';
//刷新
mysql>flush privileges;
重启mysql服务
//先退出 MySQL
mysql>exit
//重启mysql服务
[root@VM-0-2-centos ~]# service mysqld restart
然后就可以用 SQLYog 或Navicat 可视化工具 进行远程连接了