MySQL在Centos7环境安装

2023-12-14 12:17:03
说明:
? 安装与卸载中,??全部切换成为root,?旦 安装,普通??能使?的

1. 卸载不要的环境

[root@hcss-ecs-1036 ~]# 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
[root@hcss-ecs-1036 ~]# 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 ===
[root@hcss-ecs-1036 ~]# ps axj |grep mariadb # 停?完成
13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto
mariadb

2. 检查系统安装包

[root@hcss-ecs-1036 MySQL]# rpm -qa | grep mariadb
or

[root@hcss-ecs-1036 MySQL]# rpm -qa | grep mysql

?# 下?是打印出来的mysql的样例:

?3. 卸载这些默认安装包

# 卸载显?出来的 mariadb/mysql 安装包
[root@hcss-ecs-1036 ~]# 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源

# 安装前
[root@hcss-ecs-1036 ~]# ls
mysql57-community-release-el7-10.noarch.rpm

?查看你本地的yum[我的和你的肯定不同,但是没有mysql相关的yum]

?安装中

[root@hcss-ecs-1036 ~]# 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)
# 如果上?出错了
[root@hcss-ecs-1036 ~]# sudo rpm -Uvh mysql57-community-release-el7-
10.noarch.rpm
[sudo] password for wmh:
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%]
# 安装后
[root@hcss-ecs-1036 ~]# 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. 看看能不能正常?作

#在看看能不能正常?作
[root@hcss-ecs-1036 ~]# 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服务

[root@hcss-ecs-1036 ~]# sudo yum install -y mysql-community-server
[sudo] password for wmh:
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 #服务器
... #安装了服务和客?端

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

?9. 启动服务

[root@hcss-ecs-1036 ~]# 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. 查看启动服务

?11. 登陆?法?【不?就下?个】

#获取临时root密码
[wmh@hcss-ecs-1036 ~]$ 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*
#使?临时密码登录
[wmh@hcss-ecs-1036 ~]$ 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 'qwer@wu.888';
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,主要是数据库客?端和服务器的编码格式
[root@hcss-ecs-1036 ~]# 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
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8
default-storage-engine=innodb
# 配置完毕,重启mysql即可

16. 常?问题:

mysql 已经配置了客?端服务器utf8编码,但是?法输?中?
确保您在终端命令?中可以输?中?
[root@hcss-ecs-1036 ~]# 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

文章来源:https://blog.csdn.net/wmh_1234567/article/details/134911154
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。