Linux中安装mysql数据库

前言:Linux版本为Centos7.X

一、环境准备

  • Linux Centos7.X 服务器
  • linux连接工具:Xshell 7 (网上自行下载)
  • linux文件传输工具: XFTP (网上自行下载)
  • mysql版本: mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz (网上自行下载)
    mysql网盘地址:链接:https://pan.baidu.com/s/1NfeP33iWnqEwOLGFzBiVtw
    提取码:4xma

二、下载mysql

官网:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
在这里插入图片描述

三、上传文件至Linux并安装

1、上传文件

在这里插入图片描述
在这里插入图片描述

2、检查安装环境

检查否存在 mariadb 数据库,如果存在则需要卸载,防止后面安装mysql数据库出现冲突!

#检查是否存在mariadb数据库
[root@linux-study ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
#卸载mariadb数据库
[root@linux-study ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
#检查是否存在mariadb数据库
[root@linux-study ~]# rpm -qa | grep mariadb
[root@linux-study ~]# 

3、安装

1、进入安装包所在目录,解压压缩包:

[root@linux-study opt]# ls
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@linux-study opt]# tar -zxvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 

2、移动解压后的文件并重命名:

[root@linux-study opt]# mv mysql-5.7.34-linux-glibc2.12-x86_64 /usr/local/mysql
[root@linux-study opt]# cd ..
[root@linux-study /]# ls
bin   CloudResetPwdUpdateAgent  dev  home  lib64       media  opt   root  sbin  sys       tmp  var
boot  CloudrResetPwdAgent       etc  lib   lost+found  mnt    proc  run   srv   test.txt  usr
[root@linux-study /]# cd usr/
[root@linux-study usr]# ls
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@linux-study usr]# cd local/
[root@linux-study local]# ls
bin  etc  games  hostguard  include  lib  lib64  libexec  mysql  sbin  share  src  uniagent
[root@linux-study local]# cd mysql/
[root@linux-study mysql]# pwd
/usr/local/mysql
[root@linux-study mysql]# 

3、创建用户和用户组

检查用户是否存在:

[root@linux-study /]# cat /etc/group | grep mysql
[root@linux-study /]# 

创建用户和用户组:

[root@linux-study /]# groupadd mysql
[root@linux-study /]# useradd -r -g mysql mysql

创建数据库目录并授权:

[root@linux-study local]# mkdir -p  /data/mysql
[root@linux-study local]# ls
bin  etc  games  hostguard  include  lib  lib64  libexec  mysql  sbin  share  src  uniagent
[root@linux-study local]# cd /data/mysql
[root@linux-study mysql]# ls
[root@linux-study mysql]# cd ..
[root@linux-study data]# cd ..
[root@linux-study /]# chown mysql:mysql -R /data/mysql
[root@linux-study /]# 

配置my.cnf:

[root@linux-study /]# vim /etc/my.cnf

内容如下:

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql  #对应的安装目录
datadir=/data/mysql   #创建的路径
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

4、初始化数据库

进入mysql的bin目录

[root@linux-study /]# cd /usr/local/mysql/bin/

初始化数据库:

[root@linux-study bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

如果初始化出现如下错误:

./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

退回根目录,安装 libaio

[root@linux-study /]#  yum install -y libaio

安装完成后再初始化即可!!!

5、查看数据库临时密码

[root@linux-study bin]# cat /data/mysql/mysql.err

在这里插入图片描述
6、启动mysql

将mysql.server放置到/etc/init.d/mysql中

[root@linux-study bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@linux-study bin]# 

启动mysql服务:

[root@linux-study bin]# service mysql start
Starting MySQL.                                            [  OK  ]
[root@linux-study bin]#  
[root@linux-study bin]# ps -ef|grep mysql

至此!!!数据库安装完成!

四、修改数据库密码,并开通外网访问权限

1、登陆数据库

[root@linux-study bin]# ./mysql -u root -p  
Enter password: 输入前面自动生成的临时密码
mysql> 

2、重新设置密码

mysql> SET PASSWORD = PASSWORD('123456');
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql> FLUSH PRIVILEGES;                                                           

3、开通外网访问权限

mysql> use mysql      #访问mysql库
mysql> update user set host = '%' where user = 'root';  #使root用户能在任何地方都能访问数据库
mysql> FLUSH PRIVILEGES;     #刷新                               

在这里插入图片描述
完成!!!

4、拓展

1、命令无法找到解决

[root@linux-study ~]# vi /etc/ld.so.conf
-bash: vi: command not found
[root@linux-study ~]# ls
-bash: ls: command not found

解决:在命令行输入 export PATH=/bin:/usr/bin:$PATH

[root@linux-study ~]# export PATH=/bin:/usr/bin:$PATH
[root@linux-study ~]# ls
index.html

2、jdk配置加载出错

错误如下:

[root@linux-study /]# source /etc/profile
-bash: fg: %JAVA_HOME%/jre/lib: no such job
-bash: /usr/java/jdk1.8.0_221-amd64/jre/bin: Is a directory

解决:修改配置文件如下

JAVA_HOME=/usr/java/jdk1.8.0_221-amd64
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export  JAVA_HOME JRE_HOME CLASSPATH PATH

保存配置文件后,重新刷新配置即可!

[root@linux-study /]# source /etc/profile
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值