Centos7.5安装mysql5.7.23-二进制包方式安装-完美安装,一步到位
1. 环境准备
软件环境 | |
---|---|
虚拟机 | VMware Workstation Pro |
操作系统 | centos7.5 |
数据库 | mysql5.7.23 |
所有操作皆在root用户下进行.
2.下载mysql安装包
3.将文件放到虚拟机内
这里使用SecureCRTPortable连接虚拟机,使用SecureFXPortable上传文件.
3.1 新建目录mysql并将文件上传至虚拟机
- 在/usr/local/目录下创建mysql目录,上传文件至mysql
[root@BxServer001 local]# cd /usr/local/
[root@BxServer001 local]# mkdir mysql
- 上传文件
3.2 解压至mysql目录
3.2.1 切换至mysql目录并解压:
[root@BxServer001 local]# cd mysql/
[root@BxServer001 mysql]# tar -zxf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@BxServer001 mysql]# ls
mysql-5.7.23-linux-glibc2.12-x86_64
mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
3.2.2 解压成功,删除二进制文件:
[root@BxServer001 mysql]# rm mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
rm:是否删除普通文件 "mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz"?y
3.2.3 将mysql-5.7.23-linux-glibc2.12-x86_64目录下面的文件移动到mysql目录下:
[root@BxServer001 mysql]# mv mysql-5.7.23-linux-glibc2.12-x86_64/* /usr/local/mysql/
[root@BxServer001 mysql]# ls /usr/local/mysql/
bin lib share
COPYING man support-files
docs mysql-5.7.23-linux-glibc2.12-x86_64
include README
[root@BxServer001 mysql]# ls /usr/local/mysql/mysql-5.7.23-linux-glibc2.12-x86_64/
[root@BxServer001 mysql]# rm -rf /usr/local/mysql/mysql-5.7.23-linux-glibc2.12-x86_64/
[root@BxServer001 mysql]# ls
bin docs lib README support-files
COPYING include man share
4 初始化环境并配置文件
4.1 创建data文件夹
[root@BxServer001 mysql]# mkdir data
[root@BxServer001 mysql]# ls
bin data include man share
COPYING docs lib README support-files
4.2 下载依赖:
[root@BxServer001 mysql]# yum -y install libaio
4.3 创建用户组和用户并授权:
[root@BxServer001 mysql]# groupadd mysql
[root@BxServer001 mysql]# useradd -M -g mysql -s /sbin/nologin -d /usr/local/mysql mysql
[root@BxServer001 mysql]# chown -R mysql.mysql /usr/local/mysql/
4.4 卸载mariadb
centos7内部集成了mariadb,而安装mysql的话会和mariadb文件冲突,所以需要先卸载mariadb,以下为卸载mariadb,安装mysql的步骤.
#列出所有被安装的rpm package
[root@BxServer001 mysql]# rpm -qa | grep maria*
mariadb-libs-5.5.56-2.el7.x86_64
[root@BxServer001 mysql]# yum -y remove mari*
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.56-2.el7 将被 删除
......
...
删除:
mariadb-libs.x86_64 1:5.5.56-2.el7
作为依赖被删除:
postfix.x86_64 2:2.10.1-6.el7
完毕!
[root@BxServer001 mysql]# rm -rf /var/lib/mysql/*
4.5 修改my.cnf
[root@BxServer001 mysql]# vi /etc/my.cnf
#写入文件
[mysqld]
port=3306
character-set-server=utf8
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#innodb_buffer_pool_size=8M
[mysqld_safe]
log-error=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/data/mysql.pid
tmpdir = /tmp
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
#wq保存
4.6 将mysql放入系统服务,并修改对应文件
[root@BxServer001 mysql]# ls
bin data include man share
COPYING docs lib README support-files
[root@BxServer001 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@BxServer001 mysql]# vi /etc/init.d/mysqld
4.7 初始化mysql
[root@BxServer001 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
初始化成功会打印以上日志,最后红框框圈出来的是root的登陆密码.
5 启动mysql,修改root密码并设置远程连接
5.1 启动mysql
[root@BxServer001 mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/error.log'.
SUCCESS!
5.2 修改密码
5.21 登陆mysql并输入4.7初始化时打印的密码:
[root@BxServer001 mysql]# ./bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23
Copyright (c) 2000, 2018, 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>
5.22 修改root密码
mysql>SET PASSWORD FOR 'root'@localhost=PASSWORD('Your Password');
Query OK, 0 rows affected, 1 warning (0.00 sec)
5.23 设置root允许远程连接
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='root' limit 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使用Navicat Premium连接mysql
如果连接不上,检查下防火墙是否关闭或者端口3360是否放行.
6 mysql加入环境变量并设置mysql为自启服务
6.1 加入环境变量
[root@BxServer001 mysql]# vi /etc/profile
注:最后添加
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
- 执行下面的命令是修改的内容立即生效:
[root@BxServer001 mysql]# source /etc/profile
- 此时可直接使用mysql命令登陆:
[root@BxServer001 mysql]# mysql -u root -p
6.2 设置mysql为自启服务
- 赋予服务控制执行脚本的权利
[root@BxServer001 mysql]# chmod +x /etc/rc.d/init.d/mysqld
- 添加服务进系统服务
[root@BxServer001 mysql]# chkconfig --add mysqld
- 查看服务是否生效
[root@BxServer001 mysql]# chkconfig --list mysqld
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
- 开启,关闭与查看服务状态
[root@BxServer001 mysql]# systemctl start mysqld
[root@BxServer001 mysql]# systemctl stop mysqld
[root@BxServer001 mysql]# systemctl status mysqld
重启服务器,测试自启服务.