Linux下mysql安装

本文详细介绍了在Linux(CentOS)系统中安装MySQL的过程,包括下载安装包、解压、配置my.cnf文件、安装数据库、启动MySQL、设置root账号密码、允许远程连接以及开放防火墙端口。在安装过程中遇到的文件权限问题,可以通过设置用户组权限来解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


版本说明
环境:windows10 hyper
Linux:centos 8.2
mysql:8.0.20

由于博主只是想在本地的虚拟机弄一套环境出来,故没有认真的配置用户组。如配置了用户组,在安装过程中碰到问题的话。大多是文件权限问题,给予用户组权限即可。

下载

方式一:进入mysql历史版本库,选择自己需要的版本下载安装
https://downloads.mysql.com/archives/community
在这里插入图片描述
方式二:服务器有网的话直接wget下载

#下载地址可以在浏览器按f12查看
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

在这里插入图片描述

压缩包解压

配置my.cnf,

文件 /usr/my.cnf 是mysql的默认配置文件编辑文件。

vim /etc/my.cnf

内容如下:

[mysqld]
bind-address=0.0.0.0
port=3306
user=root
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
# 设置mysql的group_concat长度,默认为1024
group_concat_max_len=102400
# 设置默认sql_mode 去掉了 ONLY_FULL_GROUP_BY
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

解压、安装mysql

创建mysql数据存储文件夹

mkdir -p /data/mysql

在这里插入图片描述

解压安装文件

tar -xJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz 

在这里插入图片描述
移动解压后的文件至指定文件夹,

mv /opt/mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql/

在这里插入图片描述

安装mysql

# 安装
/usr/local/mysql/bin/mysqld --initialize --user=root --datadir=/data/mysql --basedir=/usr/local/mysql
# 查看密码,该mysql.err文件为我们上面my.cnf配置文件里面配置的
cat /data/mysql/mysql.err

在这里插入图片描述

启动mysql

/usr/local/mysql/support-files/mysql.server start

在这里插入图片描述

配置root账号自定义密码

# 登录,此处无法直接使用/usr/local/mysql/bin/mysql操作,故先进入/usr/local/mysql/bin文件夹
# mysql -u 用户名 -p登录密码
cd /usr/local/mysql/bin
mysql -u root -p1m4VWpFPHj=c
# 设置root账号自定义登录密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
# 退出校验登录
\q
mysql -u root -proot

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

设置root账号远程可连接

# 进入mysql文件夹,如果配置了mysql软链接可以跳过此步,直接使用mysql指令登录,类似于windows的环境变量
cd /usr/local/mysql/bin/
# 登录msyql
mysql -u root -proot
# 进入数据库
use mysql
# 重置密码
# update mysql.user set authentication_string=password('新密码') where user='root';
update mysql.user set authentication_string=password('123456') where user='root';
# 修改登录用户设置
update user set host='%' where user='root';
# 刷新缓存,注意,但凡是涉及到数据库角色权限之类的最后一定要执行该指令重新加载权限表,应为mysql角色权限方面默认是有缓存的
flush privileges;

在这里插入图片描述

防火墙开放端口

此时在其他服务器依然是不能连接mysql数据库的,这是由于防火墙的原因,只需要开放数据库端口或者关闭防火墙即可。
在这里插入图片描述
防火墙配置参考链接: https://blog.csdn.net/qq_37809967/article/details/109401618.防火墙设置篇

# 查看防火墙状态
firewall-cmd --state
# 查询端口是否开放
firewall-cmd --query-port=3306/tcp
# 开放3306端口
firewall-cmd --permanent --add-port=33065/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

此时已经可以连接数据库
在这里插入图片描述

异常解决

# 异常1,登录的时候报该错误
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
# 解决方案
sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
# 还是不行的话执行
yum install libncurses*

2020/12/26补充

其实mysql的安装很简单的,找到与服务器匹配的版本。丢上去基本上啥都不用配置就能干起来。毕竟你要想一想,MySQL开源面向全世界的客户。他不可能把自己的基础使用还整的很复杂。下面附加mysql.server的简陋说明

最重要的就是看看配置文件
聊聊我的尝试,
即使不配置my.cnf文件,mysql依然可以直接启动
但是配置都是隐式的,真正上线使用肯定是不够的,就是简简单单的开机自启都有点小问题,就不放出来告诉大家怎么整这个了,还是老老实实配置my.cnf文件把。

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.
# 大致意思是这个MySQL服务通常放在系统init.d文件夹里面,这样mysql服务就随着
# 系统的启动与关闭,跟着一起启动与关闭

# Comments to support chkconfig on RedHat Linux
# 在RedHat Linux上支持chkconfig的注释
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.
# 描述:一个非常快速和可靠的SQL数据库引擎。

# Comments to support LSB init script conventions
# 支持LSB init脚本约定的注释
### BEGIN INIT INFO
#   开始初始化信息
# Provides: mysql
# 提供服务:mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
# 描述:MySQL是一个非常快速和可靠的SQL数据库引擎。
### END INIT INFO
 
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
# 如果你在/usr/local/mysql外的其他位置安装MySQL,那么您必须执行以
# 下操作之一才能使此脚本正常工作:
#
# - Run this script from within the MySQL installation directory
#   从MySQL安装目录中运行此脚本,
#  (其实这个my.cnf文件是以绝对路径创建的,在哪里运行这个脚本都没事。
#   只是这里确定了位置后,下面的复制my_print_defaults文件会方便一点)
# - Create a /etc/my.cnf file with the following information:
#   创建一个包含以下信息的 /etc/my.cnf 文件
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
#   basedir=mysql服务安装地址
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
#   将上面的basedir配置添加到任何其他配置文件里面(例如 my.ini文件),
#   并且复制bin目录下的my_print_defaults文件到/usr/bin目录里面
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#   并且将mysql安装目录添加到下面的basedir配置中
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
# 如果你想修改其他的MySQL配置,你可以在my.cnf文件里面进行相应的配置。
# ./my.cnf或者其他的mysql配置文件

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
# 如果你改变了basedir的话,你必须也改变datadir。这个配置可能会被mysql配置文件覆盖

basedir=
datadir=

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 
# Value here is overriden by value in my.cnf. 
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
# 默认值,以秒为单位。服务的默认启动等待超时时间,这个值会被my.cnf配置文件覆盖
# 配置为0表示不等待
# 负数意味着无限期地等待
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
# 锁 在RedHat / SuSE系统种的文件夹
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.
# 下面一堆脚本都是为了帮助mysql服务运行的时候找到基础运行文件

# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/usr/local/mysql
  bindir=/usr/local/mysql/bin
  if test -z "$datadir"
  then
    datadir=/usr/local/mysql/data
  fi
  sbindir=/usr/local/mysql/bin
  libexecdir=/usr/local/mysql/bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH

mode=$1    # start or stop

[ $# -ge 1 ] && shift


other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                    bindir="$basedir/bin"
		    if test -z "$datadir_set"; then
		      datadir="$basedir/data"
		    fi
		    sbindir="$basedir/sbin"
		    libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
		    datadir_set=1
	;;
      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
}

wait_for_pid () {
  verb="$1"           # created | removed
  pid="$2"            # process ID of the program operating on the pid-file
  pid_file_path="$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s "$pid_file_path" && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s "$pid_file_path" && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn't running, then pid-file will never be updated
    if test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.  
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1

  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x "$bindir/my_print_defaults";  then
  print_defaults="$bindir/my_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r $conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e 's/[ 	]//g'`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
    done
  fi

  # Hope it's in the PATH ... but I doubt it
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from 'basedir'.   If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
fi

parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
  mysqld_pid_file_path=$datadir/`hostname`.pid
else
  case "$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  esac
fi

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "$mysqld_pid_file_path.shutdown"

      mysqld_pid=`cat "$mysqld_pid_file_path"`

      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid <  "$mysqld_pid_file_path"
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    # First, check to see if pid file exists
    if test -s "$mysqld_pid_file_path" ; then 
      read mysqld_pid < "$mysqld_pid_file_path"
      if kill -0 $mysqld_pid 2>/dev/null ; then 
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pidof $libexecdir/mysqld`

      # test if multiple pids exist
      pid_count=`echo $mysqld_pid | wc -w`
      if test $pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
        exit 5
      elif test -z $mysqld_pid ; then 
        if test -f "$lock_file_path" ; then 
          log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi 
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值