CentOS 安装MySQL
2020-02-01 本文已影响0人
linux_阿杰
总所周知,MySQL在我们程序员眼中那是必不可少的。
① rpm yum 安装
② 二进制安装
③ 编译安装
④ 先编译,制作rpm,制作yum库,yum安装
今天讲的是二进制安装方式
首先我们需要获取mysql5.7.26.tar.gz安装包
官网下载:https://downloads.mysql.com/archives/community/

我的已经下载完成,存放在f盘

打开我们的虚拟机,使用xshell远程连接,安装前,先检查源

安装一些依赖包
yum install -y ncurses-devel libaio-devel lszrz

开始部署环境
root@13k:/# mkdir application
root@13k:/# rz -E
root@13k:/# mv mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz application/
root@13k:/# cd application/
解压,改名
root@13k:/application# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
root@13k:/application# ls
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz mysql-5.7.26-linux-glibc2.12-x86_64
root@13k:/application# mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
root@13k:/application# ls
mysql
root@13k:/application# cd mysql
root@13k:/application/mysql# ll
总用量 36
drwxr-xr-x 2 root root 4096 2月 1 10:56 bin
-rw-r--r-- 1 7161 31415 17987 4月 13 2019 COPYING
drwxr-xr-x 2 root root 55 2月 1 10:56 docs
drwxr-xr-x 3 root root 4096 2月 1 10:56 include
drwxr-xr-x 5 root root 230 2月 1 10:56 lib
drwxr-xr-x 4 root root 30 2月 1 10:56 man
-rw-r--r-- 1 7161 31415 2478 4月 13 2019 README
drwxr-xr-x 28 root root 4096 2月 1 10:56 share
drwxr-xr-x 2 root root 90 2月 1 10:56 support-files
添加环境变量
root@13k:/application/mysql# vim /etc/profile
export PATH=/application/mysql/bin:$PATH
root@13k:/application/mysql# source /etc/profile
检查是否添加成功
root@13k:/application/mysql# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
删除系统自带的mariadb
root@13k:/application/mysql# rpm -qa |grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
root@13k:/application/mysql# yum remove mariadb-libs-5.5.56-2.el7.x86_64 -y
添加用户
useradd -s /sbin/nologin mysql
授权
root@13k:/application/mysql# chown -R mysql.mysql /application/*
root@13k:/application/mysql# cd ..
root@13k:/application# ll
总用量 0
drwxr-xr-x 9 mysql mysql 129 2月 1 10:56 mysql
进入mysql文件夹创建data数据目录
root@13k:/application/mysql# mkdir data
root@13k:/application/mysql# ls
bin COPYING data docs include lib man README share support-files
初始化数据库
mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
#指定用户 程序目录 数据目录
root@13k:/application/mysql# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
2020-02-01T03:05:35.363537Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-01T03:05:35.752400Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-01T03:05:35.821355Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-01T03:05:35.883384Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b796f373-449f-11ea-85a0-000c293e8bfe.
2020-02-01T03:05:35.885174Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-01T03:05:35.887220Z 1 [Note] A temporary password is generated for root@localhost: ke69s(/!Goov #这个就是数据库的临时密码
写入配置文件
cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql #数据库路径
datadir=/application/mysql/data #数据文件路径
socket=/tmp/mysql.sock #锁文件路径
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
配置启动脚本
root@13k:/application/mysql# cd support-files
root@13k:/application/mysql/support-files# ./mysql.server start
Starting MySQL.Logging to '/data/mysql/db01.err'.
SUCCESS!
root@13k:/application/mysql/support-files# cd /etc/init.d/
root@13k:/etc/init.d# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
重启mysql服务
root@13k:/etc/init.d# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
登录mysql
root@13k:/etc/init.d# mysql -uroot -p
输入密码
设置一个密码
mysql> set password=password("123456");

注意:虚拟机重启后,登录mysql报错提示mysql.sock 的问题,应该是启动脚本失败,手动启动mysql服务即可,或者把命令写进/etc/rc.local。