CentOS环境安装MySQL5.7

安装

  1. 下载地址
    https://dev.mysql.com/downloads/mysql/5.7.html#downloads
  2. 安装
  • 解压安装
[root@khunpean ~]# tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  • 为mysql安装目录创建软链接
ln -s mysql-5.7.30 mysql
  • 为CentOS添加mysql用户组和mysql用户(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)
[root@khunpean local]# groupadd mysql
[root@khunpean local]# useradd -r -g mysql -s /bin/false mysql
  • 修改mysql安装目录的拥有者为新建的mysql用户
[root@khunpean local]# cd /usr/local/mysql
[root@khunpean mysql]# chown -R mysql:mysql ./

  1. 安装mysql
[root@khunpean mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@khunpean mysql]# 
rpm -qa|grep libaio
yum install  libaio-devel.x86_64
yum -y install numactl
  • 解决错误后再次执行安装,记录日志最末尾位置root@localhost:后的字符串,此字符串为mysql管理员临时登录密码
2020-07-09T09:40:16.792474Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-09T09:40:17.011208Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-09T09:40:17.385758Z 1 [Note] A temporary password is generated for root@localhost: wod:Qy0cV5bS
[root@khunpean mysql]#
  1. 编辑配置文件my.cnf,添加配置如下
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
  1. 测试启动mysql服务
/usr/local/mysql/support-files/mysql.server start
  • 异常情况
[root@khunpean mysql]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.2020-07-09T09:44:29.539053Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]8vb3utiu7gwez7zb7f2fZ.pid).
[root@khunpean mysql]# 
  • 查看是否存在mysql和mysqld的服务,如果存在,则结束进程,再重新执行启动命令
#查询服务
ps -ef|grep mysql
ps -ef|grep mysqld

#结束进程
kill -9 PID

#启动服务
 /usr/local/mysql/support-files/mysql.server start
  1. 将mysql进程放入系统进程,并重启mysql服务
cp support-files/mysql.server /etc/init.d/mysqld
service mysqld restart
  1. 配置mysql环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
  1. 使用随机密码登录mysql数据库
mysql -u root -p
user mysql;
update user set authentication_string=password("新密码") where user="root";
flush privileges;
quit;
  • 在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程
  1. 设置允许远程连接数据库
use mysql;
update user set user.Host='%' where user.User='root';
select user,host from user;
flush privileges;
  • 开启3306防火墙端口,即可远程连接,如果还是无法远程连接,查看/etc/my.cnf
    找到bind-address = 127.0.0.1这一行改为bind-address = 0.0.0.0即可。
  1. 设置开机自动启动
#将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#赋予可执行权限
chmod +x /etc/init.d/mysqld
#添加服务
chkconfig --add mysqld
#显示服务列表
chkconfig --list

你可能感兴趣的:(CentOS环境安装MySQL5.7)