当前位置:   article > 正文

【MySQL】在 Centos7 环境安装 MySQL -- 详细完整教程_centos7安装mysql教程

centos7安装mysql教程

说明:

  • 安装与卸载中,用户全部切换成为 root,一旦安装,普通用户就能使用。

一、卸载内置环境

1、卸载不要的环境

  1. [root@VM-8-5-centos ~]$ ps ajx | grep mariadb # 先检查是否有mariadb存在
  2. 13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto mariadb
  3. 19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld --basedir=/usr
  4. --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-
  5. error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --
  6. socket=/var/lib/mysql/mysql.sock
  7. [root@VM-8-5-centos ~]$ systemctl stop mariadb.service # 停止mariadb服务
  8. ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
  9. Authentication is required to manage system services or units.
  10. Authenticating as: root
  11. Password:
  12. ==== AUTHENTICATION COMPLETE ===
  13. [root@VM-8-5-centos ~]$ ps axj | grep mariadb # 停止完成
  14. 13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto mariadb

mariadb 其实就是 mysql,只不过是 mysql 的另一种拉出来的开源分支,也可以正常使用,如果不想卸载也是可以的。


2、检查系统安装

  1. [root@VM-8-5-centos ~]$ rpm -qa | grep mariadb
  2. or
  3. [root@VM-8-5-centos ~]$ rpm -qa | grep mysql
  4. ...
  5. ...
  6. ...
  7. # 下面是打印出来的mysql的样例
  8. [root@VM-8-5-centos ~]# rpm -qa | grep mysql
  9. mysql-community-common-5.7.41-1.el7.x86_64
  10. mysql-community-server-5.7.41-1.el7.x86_64
  11. mysql57-community-release-el7-11.noarch
  12. mysql-community-client-5.7.41-1.el7.x86_64
  13. mysql-community-libs-5.7.41-1.el7.x86_64

3、卸载这些默认的安装包

  1. #卸载显示出来的mariadb/mysql安装包
  2. [root@VM-8-5-centos ~]$ sudo yum remove mariadb # 还是试着将上面的包都一个一个卸载吧
  3. 备份/etc/my.cnf,备份/var/lib/mysql数据

4、获取 mysql 官方 yum 源

下载 MySQL yum包:

wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm

  1. #1. 获取mysql官方yum源 http://repo.mysql.com/
  2. # 一定要看好下面的文字!如果后面自己安装不成功,就看看这个
  3. # 注意:最好安装和自己系统一致的mysql版本,否则可能会存在软件兼容性问题
  4. # 查看自己的系统版本
  5. # [root@VM-8-5-centos ~]# cat /etc/redhat-release
  6. # CentOS Linux release 7.8.2003(Core)
  7. # 可以进入 http://repo.mysql.com/,找一下和自己版本一致的资源
  8. # 下载到你的本地,然后上传到你的Linux服务器

二、 配置 MySQL yum 源

1、安装 mysql yum 源,对比前后 yum 源

安装 MySQL 源:rpm -Uvh mysql57-community-release-el7-10.noarch.rpm

  1. 安装前
  2. [root@VM-8-5-centos MySQL]$ ls
  3. mysql57-community-release-el7-10.noarch.rpm
  4. 查看你本地的yum源 [每个人的都不同,没有mysql相关的yum源]
  5. [root@VM-8-5-centos MySQL]$ ls /etc/yum.repos.d/ -al
  6. total 96
  7. drwxr-xr-x. 2 root root 4096 Apr 12 11:04 .
  8. drwxr-xr-x. 106 root root 12288 Apr 12 10:50 ..
  9. -rw-r--r-- 1 root root 2523 Dec 26 19:31 Centos-Base.repo
  10. -rw-r--r-- 1 root root 614 Nov 10 18:42 CentOS-Base.repo_bak
  11. -rw-r--r-- 1 root root 2523 Jun 16 2018 CentOS-Base.repo.bak
  12. -rw-r--r-- 1 root root 1309 Apr 8 2020 CentOS-CR.repo
  13. -rw-r--r-- 1 root root 649 Apr 8 2020 CentOS-Debuginfo.repo
  14. -rw-r--r-- 1 root root 230 Nov 10 18:42 CentOS-Epel.repo
  15. -rw-r--r-- 1 root root 314 Apr 8 2020 CentOS-fasttrack.repo
  16. -rw-r--r-- 1 root root 630 Apr 8 2020 CentOS-Media.repo
  17. -rw-r--r-- 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo
  18. -rw-r--r-- 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo
  19. -rw-r--r-- 1 root root 1331 Apr 8 2020 CentOS-Sources.repo
  20. -rw-r--r-- 1 root root 7577 Apr 8 2020 CentOS-Vault.repo
  21. -rw-r--r-- 1 root root 616 Apr 8 2020 CentOS-x86_64-kernel.repo
  22. -rw-r--r-- 1 root root 1919 Apr 8 23:57 docker-ce.repo
  23. -rw-r--r-- 1 root root 1050 Nov 1 04:33 epel.repo
  24. -rw-r--r-- 1 root root 1149 Nov 1 04:33 epel-testing.repo
  25. -rw-r--r-- 1 root root 173 Dec 9 16:08 google-chrome.repo
  26. -rw-r--r-- 1 root root 1627 Apr 5 2017 mysql-community.repo #安装mysql yum源
  27. -rw-r--r-- 1 root root 1663 Apr 5 2017 mysql-community-source.repo

2、再看看能不能正常工作

  1. [root@VM-8-5-centos MySQL]$ yum list |grep mysql
  2. Repository epel is listed more than once in the configuration
  3. mysql57-community-release.noarch el7-10 installed
  4. akonadi-mysql.x86_64 1.9.2-4.el7 base
  5. anope-mysql.x86_64 2.0.9-3.el7 epel
  6. apr-util-mysql.x86_64 1.5.2-6.el7 base
  7. calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel
  8. collectd-mysql.x86_64 5.8.1-1.el7 epel
  9. dmlite-plugins-mysql.x86_64 1.14.2-1.el7 epel
  10. dovecot-mysql.x86_64 1:2.2.36-8.el7 base

 一开始的时候我们安装的 yum,在安装完之后是可以选择删除的,因为每次 yum 操作都会更新一次,耗费时间。

rpm -qa | grep mysql

yum -y remove mysql57-community-release-el7-10.noarch


三、开始安装

1、安装 mysql 服务

安装 MySQL 服务端,需要等待一些时间:yum install -y mysql-community-server

  1. [root@VM-8-5-centos MySQL]$ sudo yum install -y mysql-community-server
  2. [sudo] password for root:
  3. Loaded plugins: aliases, auto-update-debuginfo, fastestmirror, protectbase
  4. Repository epel is listed more than once in the configuration
  5. ...
  6. Installing : mysql-community-common-5.7.33-1.el7.x86_64 #公共模块
  7. Installing : mysql-community-libs-5.7.33-1.el7.x86_64 #库
  8. Installing : mysql-community-client-5.7.33-1.el7.x86_64 #客户端
  9. Installing : mysql-community-server-5.7.33-1.el7.x86_64 #服务器
  10. ... #安装了服务和客户端
  11. # 如果报错了,看第16步《常见问题》

2、查看配置文件和数据存储位置

  1. [root@VM-8-5-centos MySQL]$ ls /etc/my.cnf
  2. /etc/my.cnf
  3. [root@VM-8-5-centos MySQL]$ sudo ls /var/lib/mysql
  4. ....

3、启动服务 

启动 MySQL:systemctl start mysqld.service

  1. [root@VM-8-5-centos MySQL]$ systemctl start mysqld.service
  2. ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
  3. Authentication is required to manage system services or units.
  4. Authenticating as: root
  5. Password:
  6. ==== AUTHENTICATION COMPLETE ===

4、查看启动服务

检查是否启动成功:systemctl status mysqld.service

  1. [root@VM-8-5-centos MySQL]$ ps axj |grep mysqld
  2. 1 23183 23182 23182 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --
  3. pid-file=/var/run/mysqld/mysqld.pid
  4. 13134 23243 23242 13134 pts/0 23242 S+ 1005 0:00 grep --color=auto mysqld
  5. [root@VM-8-5-centos MySQL]$ sudo ls -al /var/lib/mysql
  6. total 122956
  7. drwxr-x--x 5 mysql mysql 4096 Apr 12 11:23 .
  8. drwxr-xr-x. 39 root root 4096 Apr 12 11:11 ..
  9. -rw-r----- 1 mysql mysql 56 Apr 12 11:23 auto.cnf
  10. -rw------- 1 mysql mysql 1680 Apr 12 11:23 ca-key.pem
  11. -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 ca.pem
  12. -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 client-cert.pem
  13. -rw------- 1 mysql mysql 1676 Apr 12 11:23 client-key.pem
  14. -rw-r----- 1 mysql mysql 436 Apr 12 11:23 ib_buffer_pool
  15. -rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibdata1
  16. -rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile0
  17. -rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile1
  18. -rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibtmp1
  19. drwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 mysql
  20. srwxrwxrwx 1 mysql mysql 0 Apr 12 11:23 mysql.sock
  21. -rw------- 1 mysql mysql 6 Apr 12 11:23 mysql.sock.lock
  22. drwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 performance_schema
  23. -rw------- 1 mysql mysql 1680 Apr 12 11:23 private_key.pem
  24. -rw-r--r-- 1 mysql mysql 452 Apr 12 11:23 public_key.pem
  25. -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 server-cert.pem
  26. -rw------- 1 mysql mysql 1680 Apr 12 11:23 server-key.pem
  27. drwxr-x--- 2 mysql mysql 12288 Apr 12 11:23 sys

四、开始登陆

1、登陆方法一【不行就下一个】

获取临时密码,MySQL5.7 为 root 用户随机生成了一个密码:

grep 'temporary password' /var/log/mysqld.log

  1. #获取临时root密码
  2. [root@VM-8-5-centos MySQL]$ sudo grep 'temporary password' /var/log/mysqld.log
  3. 2021-04-12T03:23:46.153263Z 1 [Note] A temporary password is generated for root@localhost:
  4. yLMalT:v+5l*
  5. #使用临时密码登录
  6. [root@VM-8-5-centos MySQL]$ mysql -uroot -p
  7. Enter password:
  8. #判断修改密码时候新密码是否符合当前的策略,不满足报错,不让修改,关闭它
  9. #安全强度,默认为中,即1,要求必须包含 数字、符号、大小写字母,长度至少为8
  10. mysql> set global validate_password_policy=0;
  11. Query OK, 0 rows affected (0.00 sec)
  12. #密码最小长度
  13. mysql> set global validate_password_length=1;
  14. Query OK, 0 rows affected (0.00 sec)
  15. #修改本地登录密码,暂不授权远程登录
  16. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
  17. Query OK, 0 rows affected (0.00 sec)
  18. mysql> FLUSH PRIVILEGES;
  19. Query OK, 0 rows affected (0.00 sec)
  20. # 如果你安装最新的mysql,没有所谓的临时密码,root默认没有密码

通过临时密码登录 MySQL,进行修改密码操作:mysql -uroot -p

使用临时密码登录后,不能进行其他的操作,否则会报错,这时候我们进行修改密码操作。

因为 MySQL 的密码规则需要很复杂,我们一般自己设置的不会设置成这样,所以我们全局修改一下:

mysql> set global validate_password_policy=0;

mysql> set global validate_password_length=1;

这时候我们就可以自己设置想要的密码了。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';


2、登陆方法二【不行就下一个】

  1. # 如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码
  2. # 试着直接client登陆一下

3、登陆方法三【应该行了吧】

  1. [root@VM-8-5-centos MySQL]# vim /etc/my.cnf # 打开mysql配置文件
  2. 在[mysqld]最后一栏配置(不知道是什么,就放在配置文件最后) 加入:skip-grant-tables 选项,并保存退出
  3. [root@VM-8-5-centos MySQL]# systemctl restart mysqld # 重启mysql服务
  4. # 登陆成功
  5. [root@VM-8-5-centos MySQL]# mysql -uroot
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 7
  8. Server version: 5.7.44 MySQL Community Server (GPL)
  9. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  10. Oracle is a registered trademark of Oracle Corporation and/or its
  11. affiliates. Other names may be trademarks of their respective
  12. owners.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. mysql>

五、设置配置文件

1、设置开机启动【可以不设】

先退出 mysql 命令行,然后输入以下命令:

  1. #开启开机自启动
  2. systemctl enable mysqld
  3. systemctl daemon-reload

2、配置 my.cnf

设置 MySQL 的字符集为 UTF-8,令其支持中文:

vim /etc/my.cnf

  1. #配置一下my.conf,主要是数据库客户端和服务器的编码格式
  2. [root@VM-8-5-centos MySQL]$ cat /etc/my.cnf
  3. # For advice on how to change settings please see
  4. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
  5. [mysqld]
  6. #
  7. # Remove leading # and set to the amount of RAM for the most important data
  8. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  9. # innodb_buffer_pool_size = 128M
  10. #
  11. # Remove leading # to turn on a very important data integrity option: logging
  12. # changes to the binary log between backups.
  13. # log_bin
  14. #
  15. # Remove leading # to set options mainly useful for reporting servers.
  16. # The server defaults are faster for transactions and fast SELECTs.
  17. # Adjust sizes as needed, experiment to find the optimal values.
  18. # join_buffer_size = 128M
  19. # sort_buffer_size = 2M
  20. # read_rnd_buffer_size = 2M
  21. port=3306
  22. character-set-server=utf8
  23. default-storage-engine=innodb
  24. datadir=/var/lib/mysql
  25. socket=/var/lib/mysql/mysql.sock
  26. # Disabling symbolic-links is recommended to prevent assorted security risks
  27. symbolic-links=0
  28. character-set-server=utf8
  29. default-storage-engine=innodb
  30. log-error=/var/log/mysqld.log
  31. pid-file=/var/run/mysqld/mysqld.pid
  32. skip-grant-tables
  33. # 配置完毕,重启mysql即可

重启一下 MySQL,令配置生效:

重启 mysql:service mysqld restart

or

停止 mysql:service mysqld stop

启动 mysql:service mysqld start


3、常见问题:

  1. mysql 已经配置了客户端服务器utf8编码,但是无法输入中文
  2. 确保您在终端命令行中可以输入中文
  3. [root@VM-8-5-centos ~]$ env | grep LANG
  4. LANG=en_US.utf8
  1. 安装遇到秘钥过期的问题:
  2. Failing package is: mysql-community-client-5.7.39-1.EL7.86_64
  3. GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  4. 解决方案:
  5. rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/981933
推荐阅读
相关标签
  

闽ICP备14008679号