Centos7-Install-MYSQL

  • 环境检测

    1
    2
    3
    4
    5
    6
    7
    # 检测是否存在 mysql
    rpm -qa | grep mysql
    # 检测是否存在 mariadb
    rpm -qa grep mariadb

    # 存在删除
    rpm -e --nodeps 存在版本
  • 下载

    • 下载地址

      下载社区版本 选择社区版服务
      下载社区版本 选择社区版服务
      选择适合系统版本 进入下载页 获取下载链接
      选择适合系统版本 进入下载页 获取下载链接
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # 接下来我们在 Centos7 系统下使用 yum 命令安装 MySQL,需要注意的是 CentOS 7 版本中 MySQL数据库已从默认的程序列表中移除,所以在安装前我们需要先去官网下载 Yum 资源包,下载地址为:https://dev.mysql.com/downloads/repo/yum/
    # http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm => 5
    # https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm => 8

    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    # 安装 mysql 仓库
    rpm -ivh mysql-community-release-el7-5.noarch.rpm
    # 验证安全性(可以忽略) 和 网站提供的比较 一致则是安全的包
    md5sum mysql-community-release-el7-5.noarch.rpm
    # 查看仓库
    ls /etc/yum.repos.d
    # 更新仓库
    yum makecache
    # 查看仓库
    yum repolist
    # 更新
    yum update
    # 安装 mysql 服务(yum 可以解决依赖问题)
    yum list | grep mysql-com
    yum -y install mysql-server | yum -y install mysql-community-server.x86_64

    # 安装后位置固定
    cd /var/lib/mysql
    安装server
    安装server
  • 权限设置

    1
    2
    3
    4
    5
    # 添加用户组
    groupadd mysql
    useradd -r -g mysql mysql
    # 添加权限
    chown -R mysql:mysql /var/lib/mysql/
  • 初始化mysql

    1
    mysqld --initialize
  • 启动mysql

    1
    systemctl start mysqld
  • 查看运行状态

    1
    systemctl status mysqld
  • 验证

    1
    mysqladmin --version
  • linux 上该命令将输出以下结果,该结果基于你的系统信息

    版本检测
    版本检测
  • 登录服务器

    1
    2
    # 可以直接登录(如果未进入: grep 'password' /var/log/mysqld.log 获取临时密码 )
    [root@localhost]# mysql
    登录连接
    登录连接
  • 设置密码

    1
    [root@host]# mysqladmin -u root password "new_password";
    添加密码与登录
    添加密码与登录
  • 查看字符集

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 查看 MySQL 配置文件信息  cd /usr/share/mysql
    cat /etc/my.cnf
    # 复制该配置文件
    cp /etc/my.cnf my-cp.cnf

    show variables like '%char%';

    #编辑my.cnf配置文件
    vim /etc/my.cnf

    [client] //如果没有[client]段,就自己添加上去
    default-character-set=utf8
    [mysqld]
    character-set-server=utf8
    collation-server=utf8_general_ci
    字符集查看(创建时指定数据库字符集)
    字符集查看
  • 开放远程连接

    1
    2
    3
    use mysql;
    update user set user.Host='%' where user.User='root';
    flush privileges;
  • 安装目录问题

    1
    2
    3
    4
    5
    6
    7
    usr/bin/mysql 是指:mysql的运行路径

    var/lib/mysql 是指:mysql数据库文件的存放路径

    usr/lib/mysql 是指:mysql的安装路径

    whereis mysql
  • 开机启动

    1
    ststemctl enable mysqld
  • 忘记密码解决方案

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    vim /etc/my.cnf

    [mysqld]
    # 跳过权限表
    skip-grant-tables


    # 重启 mysql 服务
    service mysqld restart
    # 直接登录
    mysql

    # 查看字段 authentication_string => desc mysql.user;
    # 设置新密码
    update mysql.user set authentication_string=password('new_password') where user='root';
    # 刷新
    flush privileges;