本文最后更新于 2024-10-09,文章内容可能已经过时。

MySql安装方法总结

前言

MySQL version :5.7

Linux version :Centos7

(一)安装MySql:方法一

  1. 官网下载需要的包
wget \
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-client-5.7.32-1.el7.x86_64.rpm \
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-common-5.7.32-1.el7.x86_64.rpm \
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-libs-5.7.32-1.el7.x86_64.rpm \
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-libs-compat-5.7.32-1.el7.x86_64.rpm \
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-server-5.7.32-1.el7.x86_64.rpm
  1. 把他们下载到 CentOS 的 /usr/local/src 目录下
  2. 安装包,注意依赖,需要联网
yum install -y mysql-community-*-5.7.32-1.el7.x86_64.rpm
  1. 开启服务器并初始化密码,使用的命令和命令详解:
# 开启MySQL服务器
systemctl start mysqld
# 查看默认生成的密码
cat /var/log/mysqld.log | grep password

image-20211123074412791

  1. 登录MySql:
#使用你查到的临时密码进行登录
mysql -u root -p
  1. 设置密码等级和修改密码
#设置密码登记
set global validate_password_length=4;
set global validate_password_policy=0;
#修改默认密码
ALTER USER 'root'@'localhost' IDENTFIED WITH mysql_native_password BY '密码';#此处注意修改您的密码
  1. 命令查看show variables like 'character%';
  2. 需要将表中的数据改为如下:

image-20211123074223224

  • 默认表的字符集是拉丁文,后续录入中文会有乱码,所以需要将如下两个改为UTF-8

    character_set_databse

    character_set_server

  • 修改配置文件

    vim /etc/my.cnf
    
  • 修改后的内容

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    [client]
    default-character-set=utf8
    
    [mysql]
    default-character-set=utf8
    
    [mysqld]
    character-set-server=utf8
    #
    # 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
    #
    # 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
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    
  • 重点部分

    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8
    [mysqld]
    character-set-server=utf8
    

    修改后重启MySql服务