2022-07-06 21:51阅读: 1560评论: 0推荐: 0

centos8安装mysql与mysql的配置文件

0、软件版本

VMware16
CentOS8.2
MySQL:5.5.58.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

1.安装MySQL-server

[root@centos8 linux_mysql]# rpm -ivh MySQL-server-5.5.58-1.el6.x86_64.rpm --force --nodeps
警告:MySQL-server-5.5.58-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################ [100%]
准备中...                          ################################ [100%]
正在升级/安装...
   1:MySQL-server-5.5.58-1.el6        ################################ [100%]
220706 20:41:34 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
220706 20:41:34 [Note] /usr/sbin/mysqld (mysqld 5.5.58) starting as process 33380 ...
220706 20:41:34 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
220706 20:41:34 [Note] /usr/sbin/mysqld (mysqld 5.5.58) starting as process 33387 ...

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h centos8 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

2.安装mysql-client

[root@centos8 linux_mysql]# rpm -ivh MySQL-client-5.5.58-1.el6.x86_64.rpm --force --nodeps
警告:MySQL-client-5.5.58-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying...                          ################################ [100%]
准备中...                          ################################ [100%]
正在升级/安装...
   1:MySQL-client-5.5.58-1.el6        ################################ [100%]
[root@centos8 linux_mysql]#

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

[root@centos8 linux_mysql]# cat /etc/passwd|grep mysql
mysql:x:975:973:MySQL server:/var/lib/mysql:/bin/bash
[root@centos8 linux_mysql]# 
[root@centos8 linux_mysql]# cat /etc/group|grep mysql
mysql:x:973:
[root@centos8 linux_mysql]#

查看mysql版本

[root@centos8 linux_mysql]# mysqladmin --version
mysqladmin  Ver 8.42 Distrib 5.5.58, for Linux on x86_64

4、MySQL 默认没有密码,需要使用mysqladmin设置密码

/usr/bin/mysqladmin -u root password '新密码'

5、启动mysql并设置自启动

systemctl start mysqld.server

systemctl stop mysqld.server

systemctl enable mysqld.server 设置开机自启动

6、防火墙开通3306端口

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

7、报错

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

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

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

启动mysql服务时失败

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

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

image

解决:关闭selinux,重新启动

vim /etc/sysconfig/selinux
设置为disabled

image

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]#

8、mysql配置文件

查看mysql目录信息

[root@centos8 mysql]# systemctl status mysql
● mysql.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysql; generated)
   Active: active (running) since Wed 2022-07-06 22:28:26 CST; 35min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3201 ExecStop=/etc/rc.d/init.d/mysql stop (code=exited, status=0/SUCCESS)
  Process: 3294 ExecStart=/etc/rc.d/init.d/mysql start (code=exited, status=0/SUCCESS)
    Tasks: 17 (limit: 49448)
   Memory: 40.3M
   CGroup: /system.slice/mysql.service
           ├─3300 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/centos8.pid
           └─3399 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=centos8.err --pid-file=/var/lib/mysql/c>

7月 06 22:28:24 centos8 systemd[1]: Starting LSB: start and stop MySQL...
7月 06 22:28:26 centos8 mysql[3294]: Starting MySQL.. SUCCESS!
7月 06 22:28:26 centos8 systemd[1]: Started LSB: start and stop MySQL.
[root@centos8 mysql]#

centos6 应该是下面的命令

可以看到 --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/centos8.pid

  • 数据库目录(mysql数据库文件的存放路径) datadir=/var/lib/mysql
  • pid文件目录 pid-file=/var/lib/mysql/centos8.pid

mysql核心目录

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

/var/lib/mysql
/usr/lib64/mysql

mysql配置文件my.cnf(/usr/share/mysql)

  • my-huge.cnf 高端服务器 1-2G内存
  • my-large.cnf 中等规模
  • my-medium.cnf 一般
  • my-small.cnf 较小

但是,以上配置文件mysql默认不能识别,只能算是mysql配置文件的模板

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

按给定顺序从以下文件中读取:

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

使用的时候只需要将模板文件复制一份,放到默认位置就行了,mysql会自动识别
使用my-huge.cnf:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

注意:
mysql5.5默认配置文件/etc/my.cnf;
Mysql5.6默认配置文件/etc/mysql-default.cnf

参考 如何找到 MySQL my.cnf 位置

mysql日志文件路径

日志位置在/etc/my.cnf里有写。如果/etc/my.cnf配置文件里没有日志路径就用命令 find / -name 主机名.err 查。

my.cnf设置日志文件位置

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

搜索日志文件

[root@db1 ~]# find / -name db1.err
/var/lib/mysql/db1.err

查看日志文件路径

mysql> SHOW VARIABLES LIKE 'log_error';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| log_error     | ./db1.err |
+---------------+-----------+
1 row in set (0.01 sec)
mysql> show variables like 'general_log_file';
+------------------+------------------------+
| Variable_name    | Value                  |
+------------------+------------------------+
| general_log_file | /var/lib/mysql/db1.log |
+------------------+------------------------+
1 row in set (0.00 sec)
mysql>

参考日志文件路径

统一数据库字符集

查看字符集
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字符编码的区别

本文作者:jiyuchen1

本文链接:https://www.cnblogs.com/jiyuchen1/p/16452651.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   姬雨晨  阅读(1560)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起