1.卸载mariadb
[root@172-16-112-171 db_house_manage]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@172-16-112-171 db_house_manage]# yum remove mariadb
已加载插件:fastestmirror
参数 mariadb 没有匹配
不删除任何软件包
[root@172-16-112-171 db_house_manage]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@172-16-112-171 db_house_manage]# yum remove mariadb-libs-5.5.56-2.el7.x86_64
已加载插件:fastestmirror
正在解决依赖关系
2.安装mysql
2.1.准备rpm
在本地下载好上传服务器,或是使用命令下载
或者使用:
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2.2.开始安装MySQL服务器
[root@172-16-112-171 xinheng]# yum -y install mysql57-community-release-el7-10.noarch.rpm
已加载插件:fastestmirror
正在检查 mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
mysql57-community-release-el7-10.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-10 将被 安装
然后运行:yum -y install mysql-community-server
已安装:
mysql57-community-release.noarch 0:el7-10
完毕!
[root@k8s-worker xinheng]# yum -y install mysql-community-server
中途发现问题(截取一部分):
指纹 : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
软件包 : centos-release-7-5.1804.el7.centos.x86_64 (@anaconda)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql57-community-release-el7-10.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
mysql-community-common-5.7.38-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-common-5.7.38-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决方法:
[root@172-16-112-171 xinheng]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后继续安装
[root@172-16-112-171 xinheng]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@172-16-112-171 xinheng]# yum -y install mysql-community-server
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.cn99.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.38-1.el7 将被 安装
最后出现:
已安装:
mysql-community-server.x86_64 0:5.7.38-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.38-1.el7 mysql-community-common.x86_64 0:5.7.38-1.el7 mysql-community-libs.x86_64 0:5.7.38-1.el7 net-tools.x86_64 0:2.0-0.25.20131004git.el7
perl.x86_64 4:5.16.3-299.el7_9 perl-Carp.noarch 0:1.26-244.el7 perl-Encode.x86_64 0:2.51-7.el7 perl-Exporter.noarch 0:5.68-3.el7
perl-File-Path.noarch 0:2.09-2.el7 perl-File-Temp.noarch 0:0.23.01-3.el7 perl-Filter.x86_64 0:1.49-3.el7 perl-Getopt-Long.noarch 0:2.40-3.el7
perl-HTTP-Tiny.noarch 0:0.033-3.el7 perl-PathTools.x86_64 0:3.40-5.el7 perl-Pod-Escapes.noarch 1:1.04-299.el7_9 perl-Pod-Perldoc.noarch 0:3.20-4.el7
perl-Pod-Simple.noarch 1:3.28-4.el7 perl-Pod-Usage.noarch 0:1.63-3.el7 perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 perl-Socket.x86_64 0:2.010-5.el7
perl-Storable.x86_64 0:2.45-3.el7 perl-Text-ParseWords.noarch 0:3.29-4.el7 perl-Time-HiRes.x86_64 4:1.9725-3.el7 perl-Time-Local.noarch 0:1.2300-2.el7
perl-constant.noarch 0:1.27-2.el7 perl-libs.x86_64 4:5.16.3-299.el7_9 perl-macros.x86_64 4:5.16.3-299.el7_9 perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7 perl-threads.x86_64 0:1.87-4.el7 perl-threads-shared.x86_64 0:1.43-6.el7
完毕!
[root@172-16-112-171 xinheng]#
安装结束。
2.3.查看是否安装成功
[root@172-16-112-171 xinheng]# ps -ef | grep mysql
root 75271 36590 0 03:21 pts/0 00:00:00 grep --color=auto mysql
[root@172-16-112-171 xinheng]#
2.4.启动mysql
首次启动命令:systemctl start mysqld.service
查看MySQL运行状态:systemctl status mysqld.service 如下:
[root@172-16-112-171 xinheng]# systemctl start mysqld.service
[root@172-16-112-171 xinheng]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2022-06-10 03:24:22 EDT; 10s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 77669 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 77571 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 77672 (mysqld)
CGroup: /system.slice/mysqld.service
└─77672 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
6月 10 03:24:18 172-16-112-171 systemd[1]: Starting MySQL Server...
6月 10 03:24:22 172-16-112-171 systemd[1]: Started MySQL Server.
[root@172-16-112-171 xinheng]#
2.5.设置密码
先获取默认密码:(密码为root@localhost: 后面的,每个人不一样)
[root@172-16-112-171 xinheng]# cat /var/log/mysqld.log | grep password
2022-06-10T07:24:19.405311Z 1 [Note] A temporary password is generated for root@localhost: ;1rpRa9cqt_a
[root@172-16-112-171 xinheng]#
使用初始密码登录,强烈建议使用mysql -uroot -p 回车之后再输入密码,为了安全;以下是输入初始密码后的展示:
[root@172-16-112-171 xinheng]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.38
Copyright (c) 2000, 2022, 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>
然后更改设置我们自己的密码,命令为ALTER USER 'root'@'localhost' IDENTIFIED BY '你自己的密码'; (别忘了分号):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '*****';
Query OK, 0 rows affected (0.00 sec)
接着更新权限:
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
退出:(或则和Ctr+D)
mysql> exit
Bye
[root@172-16-112-171 xinheng]#
2.6.补充
2.6.1.远程连接
grant all privileges on *.* to 'root'@'192.168.0.10' identified by 'password' with grant option;
注意:命令开启的IP是 192.168.0.10,如要开启所有的,用%代替IP
mysql> grant all privileges on *.* to 'root'@'%' identified by '你自己设密码' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.6.2.开启端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
如果出现以下,开启防火墙即可,最后重新导入(firewall-cmd --reload)
[root@k8s-worker xinheng]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
FirewallD is not running
开启防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
查看防火墙状态:systemctl status firewalld
[root@k8s-worker xinheng]# systemctl start firewalld
[root@k8s-worker xinheng]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@k8s-worker xinheng]# firewall-cmd --reload
success
2.6.3.更改mysql的编码
输入:vi /etc/my.cnf 在下图指示位置添加:
纯文字如下:
[client]
default-character-set=utf8
# 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 = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
collation-server=utf8_general_ci
# 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
然后保存,关闭后,重启mysql,命令:service mysqld restart
最后检查下:
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
之后就可以连接数据库,输入汉字了。
2.6.4 sql错误
SELECT list is not in GROUP BY clause and contains nonaggregated column
解决方法:MySQL版本问题,输入:vi /etc/my.cnf命令,在最末尾添加:
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
然后,输入 systemctl restart mysqld.service 重启服务;