MySQL在Centos7环境安装

说明

  • 安装与卸载中,⽤⼾全部切换成为root,⼀旦安装,普通⽤⼾能使⽤的
  • 初期练习,mysql不进⾏⽤⼾管理,全部使⽤root进⾏,适应mysql语句,后⾯博客有⽤⼾管理,可以考虑新建普通⽤⼾

1. 卸载不要的环境

[sz@VM-0-3-centos ~]$ ps ajx |grep mariadb # 先检查是否有mariadb存在
13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto
mariadb
19010 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

[sz@VM-0-3-centos ~]$ systemctl stop mariadb.service # 停⽌mariadb 服务
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===

[sz@VM-0-3-centos ~]$ ps axj |grep mariadb # 停⽌完成
13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto
mariadb

2.检查系统安装包

[sz@VM-0-3-centos ~]$ rpm -qa | grep mariadb
or
[sz@VM-0-3-centos ~]$ rpm -qa | grep mysql
...
...
...
# 下⾯是打印出来的mysql的样例,
[root@bite-alicloud mysql]# rpm -qa | grep mysql
mysql-community-common-5.7.41-1.el7.x86_64
mysql-community-server-5.7.41-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-libs-5.7.41-1.el7.x86_64

3.卸载这些默认安装包

#卸载显⽰出来的mariadb/mysql安装包
[sz@VM-0-3-centos ~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧

备份/etc/my.cnf,备份/var/lib/mysql数据

4.获取mysql官⽅yum源

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

5. 安装mysql yum源,对⽐前后yum源

# 安装前
[sz@VM-0-3-centos 9.9]$ ls
mysql57-community-release-el7-10.noarch.rpm
查看你本地的yum源 [我的和你的肯定不同,但是没有mysql相关的yum源]
[sz@VM-0-3-centos 9.9]$ ls /etc/yum.repos.d/ -al
total 88
drwxr-xr-x. 2 root root 4096 Apr 9 15:43 .
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


##安装中
[sz@VM-0-3-centos 9.9]$ rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1
Signature, key ID 5072e1f5: NOKEY
error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission
denied)
# 如果上⾯出错了
[sz@VM-0-3-centos 9.9]$ sudo rpm -Uvh mysql57-community-release-el7-
10.noarch.rpm
[sudo] password for whb:
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1
Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql57-community-release-el7-10 ################################# [100%]


# 安装后
[sz@VM-0-3-centos 9.9]$ ls /etc/yum.repos.d/ -al
total 96
drwxr-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



6. 看看能不能正常⼯作

#在看看能不能正常⼯作
[sz@VM-0-3-centos 9.9]$ yum list |grep mysql
Repository epel is listed more than once in the configuration
mysql57-community-release.noarch el7-10 installed

akonadi-mysql.x86_64 1.9.2-4.el7 base

anope-mysql.x86_64 2.0.9-3.el7 epel

apr-util-mysql.x86_64 1.5.2-6.el7 base

calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel

collectd-mysql.x86_64 5.8.1-1.el7 epel

dmlite-plugins-mysql.x86_64 1.14.2-1.el7 epel

dovecot-mysql.x86_64 1:2.2.36-8.el7 base

7. 安装mysql服务

[sz@VM-0-3-centos 9.9]$ sudo yum install -y mysql-community-server
[sudo] password for whb:
Loaded plugins: aliases, auto-update-debuginfo, fastestmirror, protectbase
Repository 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步《常⻅问题》

8. 查看配置⽂件和数据存储位置

[sz@VM-0-3-centos 9.9]$ ls /etc/my.cnf
/etc/my.cnf
[sz@VM-0-3-centos 9.9]$ sudo ls /var/lib/mysql
.....


9. 启动服务

[sz@VM-0-3-centos 9.9]$ systemctl start mysqld.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===

10. 查看启动服务

[sz@VM-0-3-centos 9.9]$ ps axj |grep mysqld
1 23183 23182 23182 ? -1 Sl 27 0:00 /usr/sbin/mysqld --
daemonize --pid-file=/var/run/mysqld/mysqld.pid
13134 23243 23242 13134 pts/0 23242 S+ 1005 0:00 grep --color=auto mysqld

11. 登陆⽅法⼀

#获取临时root密码
[sz@VM-0-3-centos 9.9]$ sudo grep 'temporary password' /var/log/mysqld.log
2021-04-12T03:23:46.153263Z 1 [Note] A temporary password is generated for
root@localhost: yLMalT:v+5l*

#使⽤临时密码登录
[sz@VM-0-3-centos 9.9]$ mysql -uroot -p
Enter 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 '[email protected]';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
#如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

12. 登陆⽅法二

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

13. 登陆⽅法三

[root@bite-alicloud mysql]# vim /etc/my.cnf # 打开mysql配置⽂件[mysqld]最后⼀栏配置(不知道是什么,就放在配置⽂件最后) 加⼊: skip-grant-tables 选项,
并保存退出
[root@bite-alicloud mysql]# systemctl restart mysqld # 重启mysql服务

# 登陆成功
[root@bite-alicloud mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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>

14. 设置开机启动[可以不设]

#开启开机⾃启动
systemctl enable mysqld
systemctl daemon-reload

15. 配置my.cnf

#配置⼀下my.conf,主要是数据库客⼾端和服务器的编码格式
[sz@VM-0-3-centos 9.9]$ 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
[mysql]
#default-character-set=utf8 ,暂不设置,mysql有bug,汉字不回显
[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 = 2M
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
og-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8
default-storage-engine=innodb
# 配置完毕,重启mysql即可

16. 常见问题

mysql 已经配置了客⼾端服务器utf8编码,但是⽆法输⼊中⽂
确保您在终端命令⾏中可以输⼊中⽂
[whb@VM-0-3-centos ~]$ env |grep LANG
LANG=en_US.utf8
安装遇到秘钥过期的问题:
Failing package is: mysql-community-client-5.7.39-1.el7.x86_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

猜你喜欢

转载自blog.csdn.net/weixin_54183294/article/details/130227816