centos7安装mysql与mysql的配置文件

0、软件版本

VMware16
CentOS7
MySQL:8.0.42.rpm包

1、检测本机安装的MySQL

rpm -qa|grep -i mysql

2、卸载mysql

rpm -e rpm包
yum -y remove mysql*   # 如果卸载不掉,mysql可直接换成检测到的包名
rm -rf /var/lib/mysql  # 删除数据
rm /etc/my.cnf         # 删除配置文件

3、安装mysql

[root@192 ~]# rpm -ivh mysql-community-client-8.0.42-1.el7.x86_64.rpm mysql-community-common-8.0.42-1.el7.x86_64.rpm mysql-community-icu-data-files-8.0.42-1.el7.x86_64.rpm mysql-community-libs-8.0.42-1.el7.x86_64.rpm mysql-community-server-8.0.42-1.el7.x86_64.rpm mysql-community-client-plugins-8.0.42-1.el7.x86_64.rpm 
警告:mysql-community-client-8.0.42-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID a8d3785c: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-plugins-8.################################# [ 17%]
   2:mysql-community-common-8.0.42-1.e################################# [ 33%]
   3:mysql-community-libs-8.0.42-1.el7################################# [ 50%]
   4:mysql-community-client-8.0.42-1.e################################# [ 67%]
   5:mysql-community-icu-data-files-8.################################# [ 83%]
   6:mysql-community-server-8.0.42-1.e################################# [100%]
[root@192 ~]#

4、初始化数据库并修改密码

初始化数据库: 如果执行成功,会生成 data目录。如果自带了data数据那就不需要初始化了

初始化数据库有两种方法,一种是自动初始化,一种是手动初始化。

  • 手动初始化为手动执行命令初始化数据库,可根据需要选择空密码或者随机密码
  • mysql数据库安装完成之后直接运行,会自动初始化数据库
    • 有的版本或平台的mysql初始化参数不同:有的初始化是空密码,有的是随机密码,需要自己通过log文件查看一下临时密码
    • windows不会自动初始化

初始化数据库的命令

mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql

grep 'temporary password' /var/log/mysqld.log
  • --initialize 初始化数据库,同时root的临时密码,密码保存在/var/log/mysqld.log
    会生成

  • --initialize-insecure 初始化数据库,但不会生成root的临时密码,空密码

  • 如果是二进制压缩包解压安装的,需要指定datadir,否则不需要。

--user 告诉 mysqld 初始化过程使用哪个 操作系统的用户身份 来运行 MySQL 数据库的初始化过程。

  • MySQL 是以某个操作系统用户的身份来创建数据文件、设置权限等,这个用户通常是 mysql,而不是 root。
  • 如果你指定为 root,则初始化时会以 root 用户身份创建数据目录和文件,这可能导致后续 MySQL 服务启动失败(因为 MySQL 默认是以 mysql 用户运行的)
    • 所有文件将由 root:root 拥有
    • 当 MySQL 服务以 mysql 用户运行时,无法访问这些文件
    • 导致服务启动失败,报错如:Permission denied, Can't open the mysql.plugin table
  • 指定为mysql
    • MySQL 会以 mysql 操作系统用户的身份初始化数据库
    • 所有生成的数据文件(如 ibdata1, ib_logfile*, 系统表等)都会归 mysql:mysql 所有
    • 后续启动 mysqld.service 时就不会因权限问题而失败
[root@192 usr]# cd /var/lib/mysql
[root@192 mysql]# sudo mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql
[root@192 mysql]# sudo grep 'temporary password' /var/log/mysqld.log
2025-05-10T04:20:44.089846Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: AcalPp2t5h&y
[root@192 mysql]#

[root@192 mysql]# systemctl start mysqld
[root@192 mysql]#

[root@192 mysql]# mysql -uroot -p'AcalPp2t5h&y'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.42

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

[root@192 mysql]# mysqladmin -uroot -p'AcalPp2t5h&y' password 123456

5、设置root密码

方式1

先关闭密码检查

忘记密码才需要关闭密码检查,刚装好的mysql其实不需要这一步,直接下面的步骤就行了。

  • 方式1:mysqld --skip-grant-tables 使用无验证方式启动mysql服务,执行之后该窗口会一直保持执行状态,需要重新开一个窗口登录mysql

  • 方式2:在my.ini配置文件中追加以下配置

    #跳过安全检查
    skip-grant-tables
    

登录mysql

mysql -u root -p密码 如果刚装好的mysql没有密码,可以省略旧密码(-p选项)

修改root用户密码

use mysql;

update user set authentication_string=password('123456') where user='root' and Host='localhost';
-- 旧版本的Mysql 用户的密码字段可能叫password

-- ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码';

flush privileges;             #刷新权限
quit                          #退出

重启Mysql,重新登录

如果是开启的密码检查的话,需要重启服务器,否则直接重新登录mysql就行了(因为开启密码检查会禁用授权表)。

  • 如果是修改my.ini方式的话记得注释掉跳过安全检查,然后重启mysql服务。
  • 如果是mysqld --skip-grant-tables方式启动的mysql,需要关掉执行窗口,并用任务管理器结束掉mysqld进程。

方式2:使用 mysqladmin 命令修改密码

mysqladmin -u用户名 -p旧密码 password 新密码

mysqladmin -u root password '新密码'              # 如果刚装好的mysql没有密码,可以省略旧密码(`-p`选项),直接设置新密码

mysqladmin -uroot -p123456 password qweasd

执行该命令时,需要mysql服务正在运行,不能关闭mysql。

注意:不能在skip-grant-tables模式下,运行mysqladmin修改密码

6、详细信息

查看mysql版本

[root@192 ~]# mysql --version
mysql  Ver 8.0.42 for Linux on x86_64 (MySQL Community Server - GPL)
[root@192 ~]# mysqladmin --version
mysqladmin  Ver 8.0.42 for Linux on x86_64 (MySQL Community Server - GPL)
[root@192 ~]# 

查看Mysql安装时创建的mysql用户和mysql组

[root@192 ~]# cat /etc/passwd|grep mysql
mysql:x:990:985:MySQL server:/var/lib/mysql:/bin/bash
[root@192 ~]# cat /etc/group|grep mysql
mysql:x:985:
[root@192 ~]#

mysql核心目录

  • 命令目录 /usr/bin (mysql mysqladmin、mysqldump等)
  • 配置文件 /etc/my.ini
  • mysql数据库文件的存放路径 /var/lib/mysql
  • mysql启停脚本 /etc/init.d/mysql

mysql配置文件

mysql的配置文件位置:可以使用mysql --help查看,帮助文档中会有这么一句话

按照以下顺序扫描配置文件,然后从文件中读取配置:参考 如何找到 MySQL my.cnf 位置

/etc/my.cnf
/etc/mysql/my.cnf
/usr/etc/my.cnf
~/.my.cnf

默认的my.cnf配置内容:

[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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

mysql日志文件路径

my.cnf中配置日志文件位置

log-error=/var/log/mysqld.log  #错误日志路径

查看日志文件路径

mysql> SHOW VARIABLES LIKE 'log_error';
+---------------+---------------------+
| Variable_name | Value               |
+---------------+---------------------+
| log_error     | /var/log/mysqld.log |
+---------------+---------------------+
1 row in set (0.00 sec)

mysql> show variables like 'general_log_file';
+------------------+------------------------+
| Variable_name    | Value                  |
+------------------+------------------------+
| general_log_file | /var/lib/mysql/192.log |
+------------------+------------------------+
1 row in set (0.00 sec)

mysql> 
  • 错误日志 (log_error)
    • 日志内容:错误、警告、启动/关闭信息
    • 是否影响性能:几乎无影响
    • 用途:排查故障、监控异常
  • 通用日志 (general_log_file)
    • 日志内容:所有 SQL 查询及连接
    • 是否影响性能:有影响(尤其高并发)
    • 审计、调试 SQL

统一数据库字符集

查看字符集
show variables like '%char%' ; 可以发现database和server都是latin1.

修改字符集编码

打开my.cnf
    a.在[mysql]下增加:
        default-character-set=utf8
    b.在[client]下增加:
        default-character-set=utf8
    c.在[mysqld]下增加:
        character_set_server=utf8
        character_set_client=utf8
        collation_server=utf8_general_ci

修改之后需要重启mysql,或者关闭mysql服务再改

以上修改编码操作,只对修改以后的数据库生效 ,因此建议:当Mysql安装完毕后,立刻统一字符编码

mysql中的utf8与utf8mb4字符编码的区别

配置文件模板

  • MySQL 5.x 及更早版本
    这些模板文件位于源码包的 support-files/usr/share/mysql)目录中,用于根据服务器内存规模提供基础配置建议。例如:
    • my-small.cnf:适用于内存 ≤64MB 的小型服务器。
    • my-medium.cnf:适用于内存 64MB~512MB。
    • my-large.cnfmy-huge.cnf:针对更大内存的服务器。

使用的时候只需要将模板文件复制一份,放到默认位置就行了,mysql会自动识别

使用my-huge.cnf:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

  • MySQL 8.0+
    从 MySQL 8.0 开始,官方移除了这些预定义的模板文件,转而鼓励用户通过以下方式生成配置:
    1. 默认配置文件:安装后生成 /etc/my.cnf(或 /etc/mysql/my.cnf),仅包含基础配置。
    2. 动态调整:根据实际负载和硬件资源,结合监控工具优化参数。
    3. 在线工具和文档:利用官方文档或第三方工具生成个性化配置。

7、报错

防火墙开通3306端口

[root@centos8 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@centos8 ~]#

启动mysql服务时失败

systemctl status mysql.service 查看服务状态 --》 有报错 没有权限

ps -ef | grep mysql 查看服务权限

image

解决:关闭selinux,重新启动

vim /etc/sysconfig/selinux
设置为disabled

image

重置密码时提示/usr/bin/mysqladmin: 没有那个文件或目录

image

原因:yum安装GPG keys ,不安装的话,安装mysql rpm包时要加 --force --nodeps

解决办法参考 -BASH: /USR/BIN/MYSQLADMIN: /LIB/LD-LINUX.SO.2: BAD ELF INTERPRETER: 没有那个文件或目录

mysql登录时报错 找不到 libncurses.so.5 / libtinfo.so.5

解决办法:建立一个软链接到libncurses.so.6.1 和 libtinfo.so.6.1

[root@centos8 ~]# mysql -uroot -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@centos8 ~]# cd /usr/lib64
[root@centos8 lib64]# sudo ln -s libncurses.so.6.1 /usr/lib64/libncurses.so.5
[root@centos8 lib64]# ls -l /usr/lib64 | grep -i libncurses
lrwxrwxrwx   1 root root       17 7月   6 22:04 libncurses.so.5 -> libncurses.so.6.1
lrwxrwxrwx.  1 root root       17 5月  11 2019 libncurses.so.6 -> libncurses.so.6.1
-rwxr-xr-x.  1 root root   216912 5月  11 2019 libncurses.so.6.1
lrwxrwxrwx.  1 root root       18 5月  11 2019 libncursesw.so.6 -> libncursesw.so.6.1
-rwxr-xr-x.  1 root root   300104 5月  11 2019 libncursesw.so.6.1
[root@centos8 lib64]#

[root@centos8 ~]# mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
[root@centos8 ~]# ls -l /usr/lib64 | grep -i libtinfo
lrwxrwxrwx.  1 root root       15 5月  11 2019 libtinfo.so.6 -> libtinfo.so.6.1
-rwxr-xr-x.  1 root root   208616 5月  11 2019 libtinfo.so.6.1
[root@centos8 ~]# cd /usr/lib64
[root@centos8 lib64]# 
[root@centos8 lib64]# sudo ln -s libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
[root@centos8 lib64]#

此方法参考mysql时报错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object fil

ERROR 1045 (28000) 原因:用户名密码不正确 或 没有权限

[root@centos8 lib64]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@centos8 lib64]#

报错原因参考:mysql登录报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

mysql.sock不存在,原因:mysql服务未启动

设置新密码,需要启动mysql服务,和windows不一样

[root@centos8 lib64]# /usr/bin/mysqladmin -u root password '12345678';
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!
[root@centos8 lib64]#systemctl start mysql
[root@centos8 lib64]# /usr/bin/mysqladmin -u root password '12345678';
[root@centos8 lib64]#
posted @ 2022-07-06 21:51  姬雨晨  阅读(1677)  评论(0)    收藏  举报