行业知识
【MySQL】在 Centos7 环境安装 MySQL -- 详细完整教程
说明:
- 安装与卸载中,用户全部切换成为 root,一旦安装,普通用户就能使用。
一、卸载内置环境
1、卸载不要的环境
[root@VM-8-5-centos ~]$ ps ajx | grep mariadb # 先检查是否有mariadb存在13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto mariadb19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld --basedir=/usr--datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock[root@VM-8-5-centos ~]$ systemctl stop mariadb.service # 停止mariadb服务==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===Authentication is required to manage system services or units.Authenticating as: rootPassword:==== AUTHENTICATION COMPLETE ===[root@VM-8-5-centos ~]$ ps axj | grep mariadb # 停止完成13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto mariadb
mariadb 其实就是 mysql,只不过是 mysql 的另一种拉出来的开源分支,也可以正常使用,如果不想卸载也是可以的。
2、检查系统安装包
[root@VM-8-5-centos ~]$ rpm -qa | grep mariadbor[root@VM-8-5-centos ~]$ rpm -qa | grep mysql.........# 下面是打印出来的mysql的样例[root@VM-8-5-centos ~]# rpm -qa | grep mysqlmysql-community-common-5.7.41-1.el7.x86_64mysql-community-server-5.7.41-1.el7.x86_64mysql57-community-release-el7-11.noarchmysql-community-client-5.7.41-1.el7.x86_64mysql-community-libs-5.7.41-1.el7.x86_64
3、卸载这些默认的安装包
#卸载显示出来的mariadb/mysql安装包[root@VM-8-5-centos ~]$ sudo yum remove mariadb # 还是试着将上面的包都一个一个卸载吧备份/etc/my.cnf,备份/var/lib/mysql数据
4、获取 mysql 官方 yum 源
下载 MySQL yum包:
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
#1. 获取mysql官方yum源 http://repo.mysql.com/# 一定要看好下面的文字!如果后面自己安装不成功,就看看这个# 注意:最好安装和自己系统一致的mysql版本,否则可能会存在软件兼容性问题# 查看自己的系统版本# [root@VM-8-5-centos ~]# cat /etc/redhat-release# CentOS Linux release 7.8.2003(Core)# 可以进入 http://repo.mysql.com/,找一下和自己版本一致的资源# 下载到你的本地,然后上传到你的Linux服务器
二、 配置 MySQL yum 源
1、安装 mysql yum 源,对比前后 yum 源
安装 MySQL 源:rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
安装前[root@VM-8-5-centos MySQL]$ lsmysql57-community-release-el7-10.noarch.rpm查看你本地的yum源 [每个人的都不同,没有mysql相关的yum源][root@VM-8-5-centos MySQL]$ ls /etc/yum.repos.d/ -altotal 96drwxr-xr-x. 2 root root 4096 Apr 12 11:04 .drwxr-xr-x. 106 root root 12288 Apr 12 10:50 ..-rw-r--r-- 1 root root 2523 Dec 26 19:31 Centos-Base.repo-rw-r--r-- 1 root root 614 Nov 10 18:42 CentOS-Base.repo_bak-rw-r--r-- 1 root root 2523 Jun 16 2018 CentOS-Base.repo.bak-rw-r--r-- 1 root root 1309 Apr 8 2020 CentOS-CR.repo-rw-r--r-- 1 root root 649 Apr 8 2020 CentOS-Debuginfo.repo-rw-r--r-- 1 root root 230 Nov 10 18:42 CentOS-Epel.repo-rw-r--r-- 1 root root 314 Apr 8 2020 CentOS-fasttrack.repo-rw-r--r-- 1 root root 630 Apr 8 2020 CentOS-Media.repo-rw-r--r-- 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo-rw-r--r-- 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo-rw-r--r-- 1 root root 1331 Apr 8 2020 CentOS-Sources.repo-rw-r--r-- 1 root root 7577 Apr 8 2020 CentOS-Vault.repo-rw-r--r-- 1 root root 616 Apr 8 2020 CentOS-x86_64-kernel.repo-rw-r--r-- 1 root root 1919 Apr 8 23:57 docker-ce.repo-rw-r--r-- 1 root root 1050 Nov 1 04:33 epel.repo-rw-r--r-- 1 root root 1149 Nov 1 04:33 epel-testing.repo-rw-r--r-- 1 root root 173 Dec 9 16:08 google-chrome.repo-rw-r--r-- 1 root root 1627 Apr 5 2017 mysql-community.repo #安装mysql yum源-rw-r--r-- 1 root root 1663 Apr 5 2017 mysql-community-source.repo
2、再看看能不能正常工作
[root@VM-8-5-centos MySQL]$ yum list |grep mysqlRepository epel is listed more than once in the configurationmysql57-community-release.noarch el7-10 installedakonadi-mysql.x86_64 1.9.2-4.el7 baseanope-mysql.x86_64 2.0.9-3.el7 epelapr-util-mysql.x86_64 1.5.2-6.el7 basecalligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epelcollectd-mysql.x86_64 5.8.1-1.el7 epeldmlite-plugins-mysql.x86_64 1.14.2-1.el7 epeldovecot-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
[root@VM-8-5-centos MySQL]$ sudo yum install -y mysql-community-server[sudo] password for root:Loaded plugins: aliases, auto-update-debuginfo, fastestmirror, protectbaseRepository epel is listed more than once in the configuration... Installing : mysql-community-common-5.7.33-1.el7.x86_64 #公共模块 Installing : mysql-community-libs-5.7.33-1.el7.x86_64 #库 Installing : mysql-community-client-5.7.33-1.el7.x86_64 #客户端 Installing : mysql-community-server-5.7.33-1.el7.x86_64 #服务器... #安装了服务和客户端# 如果报错了,看第16步《常见问题》
2、查看配置文件和数据存储位置
[root@VM-8-5-centos MySQL]$ ls /etc/my.cnf/etc/my.cnf[root@VM-8-5-centos MySQL]$ sudo ls /var/lib/mysql....
3、启动服务
启动 MySQL:systemctl start mysqld.service
[root@VM-8-5-centos MySQL]$ systemctl start mysqld.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===Authentication is required to manage system services or units.Authenticating as: rootPassword:==== AUTHENTICATION COMPLETE ===
4、查看启动服务
检查是否启动成功:systemctl status mysqld.service
[root@VM-8-5-centos MySQL]$ ps axj |grep mysqld1 23183 23182 23182 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid13134 23243 23242 13134 pts/0 23242 S+ 1005 0:00 grep --color=auto mysqld[root@VM-8-5-centos MySQL]$ sudo ls -al /var/lib/mysqltotal 122956drwxr-x--x 5 mysql mysql 4096 Apr 12 11:23 .drwxr-xr-x. 39 root root 4096 Apr 12 11:11 ..-rw-r----- 1 mysql mysql 56 Apr 12 11:23 auto.cnf-rw------- 1 mysql mysql 1680 Apr 12 11:23 ca-key.pem-rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 ca.pem-rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 client-cert.pem-rw------- 1 mysql mysql 1676 Apr 12 11:23 client-key.pem-rw-r----- 1 mysql mysql 436 Apr 12 11:23 ib_buffer_pool-rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibdata1-rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile0-rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile1-rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibtmp1drwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 mysqlsrwxrwxrwx 1 mysql mysql 0 Apr 12 11:23 mysql.sock-rw------- 1 mysql mysql 6 Apr 12 11:23 mysql.sock.lockdrwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 performance_schema-rw------- 1 mysql mysql 1680 Apr 12 11:23 private_key.pem-rw-r--r-- 1 mysql mysql 452 Apr 12 11:23 public_key.pem-rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 server-cert.pem-rw------- 1 mysql mysql 1680 Apr 12 11:23 server-key.pemdrwxr-x--- 2 mysql mysql 12288 Apr 12 11:23 sys
四、开始登陆
1、登陆方法一【不行就下一个】
获取临时密码,MySQL5.7 为 root 用户随机生成了一个密码:
grep 'temporary password' /var/log/mysqld.log
#获取临时root密码[root@VM-8-5-centos MySQL]$ sudo grep 'temporary password' /var/log/mysqld.log2021-04-12T03:23:46.153263Z 1 [Note] A temporary password is generated for root@localhost:yLMalT:v+5l*#使用临时密码登录[root@VM-8-5-centos MySQL]$ mysql -uroot -pEnter password:#判断修改密码时候新密码是否符合当前的策略,不满足报错,不让修改,关闭它#安全强度,默认为中,即1,要求必须包含 数字、符号、大小写字母,长度至少为8位mysql> set global validate_password_policy=0;Query OK, 0 rows affected (0.00 sec)#密码最小长度mysql> set global validate_password_length=1;Query OK, 0 rows affected (0.00 sec)#修改本地登录密码,暂不授权远程登录mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)# 如果你安装最新的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、登陆方法二【不行就下一个】
# 如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码# 试着直接client登陆一下
3、登陆方法三【应该行了吧】
[root@VM-8-5-centos MySQL]# vim /etc/my.cnf # 打开mysql配置文件在[mysqld]最后一栏配置(不知道是什么,就放在配置文件最后) 加入:skip-grant-tables 选项,并保存退出[root@VM-8-5-centos MySQL]# systemctl restart mysqld # 重启mysql服务# 登陆成功[root@VM-8-5-centos MySQL]# mysql -urootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7Server version: 5.7.44 MySQL Community Server (GPL)Copyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
五、设置配置文件
1、设置开机启动【可以不设】
先退出 mysql 命令行,然后输入以下命令:
#开启开机自启动systemctl enable mysqldsystemctl daemon-reload
2、配置 my.cnf
设置 MySQL 的字符集为 UTF-8,令其支持中文:
vim /etc/my.cnf
#配置一下my.conf,主要是数据库客户端和服务器的编码格式[root@VM-8-5-centos MySQL]$ cat /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[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## 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 = 2Mport=3306character-set-server=utf8default-storage-engine=innodbdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0character-set-server=utf8default-storage-engine=innodblog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidskip-grant-tables# 配置完毕,重启mysql即可
重启一下 MySQL,令配置生效:
重启 mysql:service mysqld restart
or
停止 mysql:service mysqld stop
启动 mysql:service mysqld start
3、常见问题:
mysql 已经配置了客户端服务器utf8编码,但是无法输入中文确保您在终端命令行中可以输入中文[root@VM-8-5-centos ~]$ env | grep LANGLANG=en_US.utf8
安装遇到秘钥过期的问题:Failing package is: mysql-community-client-5.7.39-1.EL7.86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql解决方案:rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022