腾讯云轻量应用服务器安装MySQL5.7。


云服务商:腾讯云

产品:轻量应用云服务器

系统:CentOS 7.6

1.前期准备

1.下载MySQL5.7安装包到本地环境

  1. 下载地址:https://dev.mysql.com/downloads/mysql/
  2. 点击Looking for previous GA versions?选择想要的版本。
  3. 选择好版本后进行下载。

2.上传MySQL5.7安装包到云服务器

  1. 使用su命令切换到root用户,以获得足够的操作权限(会提示输入登录密码)

  2. 在/usr/local路径下新建mysql目录:

    mkdir /usr/local/mysql
  3. 使用WinSCP将下载的MySQL5.7安装包上传到刚才新建的mysql目录

  4. 查看是否上传成功:

    cd /usr/local/mysql #跳转到mysql目录
    ls #查看mysql目录里是否有上传的mysql安装包

2.安装步骤

1.查看linux下是否有老版本的mysql(有删除)

  1. 查找是否有旧的mysql:

    rpm -qa | grep mysql
  2. 卸载(有旧的mysql情况下):

    #卸载命令:rpm –ev {包名}
    rpm -ev mysql-community-common-5.7.23-1.el7.x86_64
  3. 查找老版本mysql相关的安装目录命令:

    find / -name mysql
  4. 删除目录(若查找到相关目录):

    rm –rf {目录名}

2.查看 linux 下是否安装 mariadb 数据库(有的话需要删除,因为有冲突)

  1. 检查是否安装了 mariadb:

    rpm -qa | grep mariadb
  2. 删除mariadb(如果安装了的情况下):

    rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

3.解压安装

  1. 跳转到存放安装包的目录

    cd /usr/local/mysql
  2. 解压安装包

    tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz #替换为自己的安装包名称
  3. 移动并修改名字,将解压目录里的所有文件剪切到mysql目录:

    mv mysql-5.7.24-linux-glibc2.12-x86_64/* /usr/local/mysql
  4. 删除解压的目录(此时目录所有文件被剪切到mysql目录,解压的目录为空)

    rm -rf mysql-5.7.24-linux-glibc2.12-x86_64
  5. 创建主目录(data:存储目录)

    mkdir /usr/local/mysql/data
  6. 主目录权限处理(查看是否有就得用户,有删除并新建用户)

    #查看组和用户情况:
    cat /etc/group | grep mysql
    #查看组和用户情况:
    cat /etc/passwd |grep mysql

    #若存在,则删除原mysql用户:
    userdel -r mysql #会删除其对应的组和用户并在次查看。

    #创建mysql组:
    groupadd mysql
    #创建mysql用户:
    useradd -r -g mysql mysql
    #修改目录拥有者:
    chown -R mysql:mysql /usr/local/mysql
  7. 创建配置文件,并配置

    #创建my.cnf文件,并打开
    vim /etc/my.cnf

    点击 i 键进入编辑模式,将文件模板复制粘贴在my.cnf中,文件模板:

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.

    [mysqld]

    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M

    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin

    # These are commonly set, remove the # and set as required.
    # basedir = .....
    # datadir = .....
    # port = .....
    # server_id = .....
    # socket = .....

    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    修改配置文件内容,注意要在模板的 [mysqld] 下面去修改(basedir:mysql安装路径,datadir:数据存储目录),修改为:

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.

    [mysqld]

    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M

    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin

    # These are commonly set, remove the # and set as required.
    #=========配置相关路径等信息==========
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    port = 3306
    socket=/tmp/mysql.sock
    pid-file=/tmp/mysqld/mysqld.pid
    character-set-server = utf8
    log-error=/var/log/mysqld.log

    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    按esc键退出回到命令模式,输入:

    :wq

    保存并退出。

  8. 创建刚才配置相关文件

    #创建文件/tmp/mysql.sock:设置用户组及用户,授权
    cd /tmp
    touch mysql.sock
    chown mysql:mysql mysql.sock
    chmod 755 mysql.sock

    #创建文件/tmp/mysqld/mysqld.pid
    mkdir mysqld
    cd mysqld
    touch mysqld.pid
    cd ..
    chown -R mysql:mysql mysqld
    cd mysqld
    chmod 755 mysqld.pid

    #创建文件/var/log/mysqld.log:
    touch /var/log/mysqld.log
    chown -R mysql:mysql /var/log
    cd /var/log
    chmod 755 mysqld.log
  9. 安装和初始化数据库

    #进入初始化目录:
    cd /usr/local/mysql/bin/

    #初始化数据库:
    ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data

    #如果报错:(./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory)
    #需要安装命令:
    yum -y install numactl

    #之后在执行初始化数据库:
    ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data
  10. 安全启动:

    #进入到bin目录,
    cd /usr/local/mysql/bin/
    #安全启动
    ./mysqld_safe --user=mysql &
    #查看是否成功:
    ps -ef | grep mysql

    #默认密码在mysqld.log日志里, 找到后保存到安全的地方:
    cat /var/log/mysqld.log
    #其中root@localhost: 后面的就是默认密码,后面登录用(D;J.ogLj8ETr)

    #进入bin目录:
    cd /usr/local/mysql/bin/

    #登录mysql:
    ./mysql -u root -p #之后会提示输入刚才看到的默认密码

    #输入mysql相关命令,会提示你修改用户密码(注意后面mysql命令一定要加“;”)。
    show databases;

    #密码修改为 aaa
    mysql> set password=password("aaa");

    #再次输入mysql相关命令
    show databases; #此时会成功看到默认的数据库表
  11. 设置远程登录权限(在mysql里面设置)

    mysql>grant all privileges on *.* to 'root'@'%' identified by 'aaa';

    #刷新登录权限:
    mysql> flush privileges;

    #退出quit 或者 exit
    mysql> quit;
  12. 开机服务启动设置(之后开机自动启动)

    #把support-files/mysql.server 拷贝为/etc/init.d/mysql:
    cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

    #查看是否成功:(名字为mysql)
    cd /etc/init.d/
    ll

    #查看mysql服务是否在服务配置中
    chkconfig --list mysql

    #若没有,则把mysql注册为开机启动的服务,然后在进行查看
    chkconfig --add mysql
    chkconfig --list mysql

    #启动 或 停止
    service mysql start
    service mysql stop
    #当发现不能停止服务时,可以查看进程id,并通过进程id强制杀死服务进程
  13. 创建快捷方式,服务启动后,直接运行mysql -u root -p即可登录,不需要进入到对应的目录。

    ln -s /usr/local/mysql/bin/mysql /usr/bin

  14. 若使用Navicat 远程连接数据库时会出现(2003)
    说明你的防火墙没有关。


如有错误,欢迎评论区留言!