文章目录1. 卸载不要的环境2. 检查系统安装包3. 卸载这些默认安装包4. 获取mysql官方yum源5. 安装mysql yum 源对比前后yum源6. 看看能不能正常工作7. 安装mysql服务8. 查看配置文件和数据存储位置9. 启动服务10. 查看启动服务11. 登陆方法一【不行就下一个】12. 登陆方法二【不行就下一个】13. 登陆方法三【应该行了吧】14. 设置开机启动[可以不设]15. 配置my.cnf16. 常见问题说明安装与卸载中用户全部切换成为root一旦安装普通用户能使用的初期练习mysql不进行用户管理全部使用root进行尽快适应mysql语句后面学了用户管理在考虑新建普通用户1. 卸载不要的环境[whbVM-0-3-centos~]$ ps ajx|grep mariadb # 先检查是否有mariadb存在13134148441484313134pts/014843S10050:00grep--colorautomariadb19010191871901019010?-1Sl2716: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[whbVM-0-3-centos~]$ systemctl stop mariadb.service # 停⽌mariadb 服务AUTHENTICATING FOR org.freedesktop.systemd1.manage-unitsAuthentication is required to manage system services or units.Authenticating as:root Password:AUTHENTICATION COMPLETE[whbVM-0-3-centos~]$ ps axj|grep mariadb # 停⽌完成13134149761497513134pts/014975S10050:00grep--colorautomariadb[rootVM-4-3-centos~]# ps-axj|grep mysql3347358335823347pts/03582S00:00grep--colorautomysql[rootVM-4-3-centos~]# ps-axj|grep mariadb3347364436433347pts/03643S00:00grep--colorautomariadb2. 检查系统安装包[whbVM-0-3-centos~]$ rpm-qa|grep mariadb or[whbVM-0-3-centos~]$ rpm-qa|grep mysql.........# 下⾯是打印出来的mysql的样例[rootbite-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_643. 卸载这些默认安装包#卸载显⽰出来的mariadb/mysql安装包[whbVM-0-3-centos~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧 备份/etc/my.cnf,备份/var/lib/mysql数据4. 获取mysql官方yum源#1.获取mysql官⽅yum源 http://repo.mysql.com/# ⼀定要看下⾯的⽂字如果后⾯⾃⼰安装不成功就看看这个 # 注意最好安装和⾃⼰系统⼀致的mysql版本否则可能会存在软件兼容性问题 # 查看⾃⼰的系统版本 #[rootVM-0-2-centos~]# cat/etc/redhat-release#CentOS Linux release7.8.2003(Core)# 可以进⼊ http://repo.mysql.com/ 找⼀下和⾃⼰版本⼀致的资源# 下载到你的本地然后上传到你的Linux服务器5. 安装mysql yum 源对比前后yum源# 安装前[whbVM-0-3-centos9.9]$ ls mysql57-community-release-el7-10.noarch.rpm 查看你本地的yum源[我的和你的肯定不同但是没有mysql相关的yum源][whbVM-0-3-centos9.9]$ ls/etc/yum.repos.d/-al total88drwxr-xr-x.2root root4096Apr915:43.drwxr-xr-x.106root root12288Apr1210:50..-rw-r--r--1root root2523Dec2619:31Centos-Base.repo-rw-r--r--1root root614Nov1018:42CentOS-Base.repo_bak-rw-r--r--1root root2523Jun162018CentOS-Base.repo.bak-rw-r--r--1root root1309Apr82020CentOS-CR.repo-rw-r--r--1root root649Apr82020CentOS-Debuginfo.repo-rw-r--r--1root root230Nov1018:42CentOS-Epel.repo-rw-r--r--1root root314Apr82020CentOS-fasttrack.repo-rw-r--r--1root root630Apr82020CentOS-Media.repo-rw-r--r--1root root998Dec112018CentOS-SCLo-scl.repo-rw-r--r--1root root971Oct292018CentOS-SCLo-scl-rh.repo-rw-r--r--1root root1331Apr82020CentOS-Sources.repo-rw-r--r--1root root7577Apr82020CentOS-Vault.repo-rw-r--r--1root root616Apr82020CentOS-x86_64-kernel.repo-rw-r--r--1root root1919Apr823:57docker-ce.repo-rw-r--r--1root root1050Nov104:33epel.repo-rw-r--r--1root root1149Nov104:33epel-testing.repo-rw-r--r--1root root173Dec916:08google-chrome.repo ##安装中[whbVM-0-3-centos9.9]$ rpm-Uvh mysql57-community-release-el7-10.noarch.rpm warning:mysql57-community-release-el7-10.noarch.rpm:Header V3 DSA/SHA1 Signature,key ID5072e1f5:NOKEY error:cant create transaction lock on/var/lib/rpm/.rpm.lock(Permission denied)# 如果上⾯出错了[whbVM-0-3-centos9.9]$ sudo rpm-Uvh mysql57-community-release-el7-10.noarch.rpm[sudo]passwordforwhb:warning:mysql57-community-release-el7-10.noarch.rpm:Header V3 DSA/SHA1 Signature,key ID5072e1f5:NOKEY Preparing...#################################[100%]Updating/installing...1:mysql57-community-release-el7-10#################################[100%]# 安装后[whbVM-0-3-centos9.9]$ ls/etc/yum.repos.d/-al total96drwxr-xr-x.2root root4096Apr1211:04.drwxr-xr-x.106root root12288Apr1210:50..-rw-r--r--1root root2523Dec2619:31Centos-Base.repo-rw-r--r--1root root614Nov1018:42CentOS-Base.repo_bak-rw-r--r--1root root2523Jun162018CentOS-Base.repo.bak-rw-r--r--1root root1309Apr82020CentOS-CR.repo-rw-r--r--1root root649Apr82020CentOS-Debuginfo.repo-rw-r--r--1root root230Nov1018:42CentOS-Epel.repo-rw-r--r--1root root314Apr82020CentOS-fasttrack.repo-rw-r--r--1root root630Apr82020CentOS-Media.repo-rw-r--r--1root root998Dec112018CentOS-SCLo-scl.repo-rw-r--r--1root root971Oct292018CentOS-SCLo-scl-rh.repo-rw-r--r--1root root1331Apr82020CentOS-Sources.repo-rw-r--r--1root root7577Apr82020CentOS-Vault.repo-rw-r--r--1root root616Apr82020CentOS-x86_64-kernel.repo-rw-r--r--1root root1919Apr823:57docker-ce.repo-rw-r--r--1root root1050Nov104:33epel.repo-rw-r--r--1root root1149Nov104:33epel-testing.repo-rw-r--r--1root root173Dec916:08google-chrome.repo-rw-r--r--1root root1627Apr52017mysql-community.repo #安装了 mysql yum源-rw-r--r--1root root1663Apr52017mysql-community-source.repo6. 看看能不能正常工作#再看看能不能正常⼯作[whbVM-0-3-centos9.9]$ yum list|grep mysql Repository epel is listed more than once in the configuration mysql57-community-release.noarch el7-10installed akonadi-mysql.x86_641.9.2-4.el7 base anope-mysql.x86_642.0.9-3.el7 epel apr-util-mysql.x86_641.5.2-6.el7 base calligra-kexi-driver-mysql.x86_642.9.10-2.el7 epel collectd-mysql.x86_645.8.1-1.el7 epel dmlite-plugins-mysql.x86_641.14.2-1.el7 epel dovecot-mysql.x86_641:2.2.36-8.el7 base7. 安装mysql服务[whbVM-0-3-centos9.9]$ sudo yum install-y mysql-community-server[sudo]passwordforwhb: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. 查看配置文件和数据存储位置[whbVM-0-3-centos9.9]$ ls/etc/my.cnf/etc/my.cnf[whbVM-0-3-centos9.9]$ sudo ls/var/lib/mysql....9. 启动服务[whbVM-0-3-centos9.9]$ systemctl start mysqld.serviceAUTHENTICATING FOR org.freedesktop.systemd1.manage-unitsAuthentication is required to manage system services or units.Authenticating as:root Password:AUTHENTICATION COMPLETE10. 查看启动服务[whbVM-0-3-centos9.9]$ ps axj|grep mysqld1231832318223182?-1Sl270:00/usr/sbin/mysqld--daemonize--pid-file/var/run/mysqld/mysqld.pid13134232432324213134pts/023242S10050:00grep--colorautomysqld11. 登陆方法一【不行就下一个】#获取临时root密码[whbVM-0-3-centos9.9]$ sudo greptemporary password/var/log/mysqld.log2021-04-12T03:23:46.153263Z1[Note]A temporary password is generatedforrootlocalhost:yLMalT:v5l*#使⽤临时密码登录[whbVM-0-3-centos9.9]$ mysql-uroot-p Enter password:#判断修改密码时候新密码是否符合当前的策略不满⾜报错不让修改关闭它 #安全强度默认为中即1要求必须包含 数字、符号、⼤⼩写字⺟⻓度⾄少为8位 mysqlset global validate_password_policy0;Query OK,0rowsaffected(0.00sec)#密码最⼩⻓度 mysqlset global validate_password_length1;Query OK,0rowsaffected(0.00sec)#修改本地登录密码暂不授权远程登录 mysqlALTER USERrootlocalhostIDENTIFIED BYqwerwu.888;Query OK,0rowsaffected(0.00sec)mysqlFLUSH PRIVILEGES;Query OK,0rowsaffected(0.00sec)#如果你安装的最新的 mysql没有所谓的临时密码root默认没有密码12. 登陆方法二【不行就下一个】# 如果你安装的最新的mysql没有所谓的临时密码root默认没有密码 # 试着直接client登陆⼀下13. 登陆方法三【应该行了吧】[rootbite-alicloud mysql]# vim/etc/my.cnf # 打开mysql配置⽂件 在[mysqld]最后⼀栏配置(不知道是什么就放在配置⽂件最后)加⼊:skip-grant-tables 选项,并保存退出[rootbite-alicloud mysql]# systemctl restart mysqld # 重启mysql服务 # 登陆成功[rootbite-alicloud mysql]# mysql-uroot Welcome to the MySQL monitor.Commands end with;or \g.Your MySQL connection id is2Server version:5.7.41MySQL CommunityServer(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.Typehelp;or\hforhelp.Type\cto clear the current input statement.mysql14. 设置开机启动[可以不设]#开启开机⾃启动 systemctl enable mysqld systemctl daemon-reload15. 配置my.cnf#配置⼀下my.conf,主要是数据库客⼾端和服务器的编码格式[whbVM-0-3-centos9.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-setutf8 暂不设置mysql有bug汉字不回显[mysqld]##Remove leading # and set to the amount of RAMforthe most important data#cachein MySQL.Start at70%of total RAMfordedicated server,else10%.#innodb_buffer_pool_size128M##Remove leading # to turn on a very important data integrity option:logging#changesto the binary log between backups.#log_bin##Remove leading # to set options mainly usefulforreporting servers.#The server defaults are fasterfortransactions and fast SELECTs.#Adjust sizes as needed,experiment to find the optimal values.#join_buffer_size128M#sort_buffer_size2M#read_rnd_buffer_size2Mport3306datadir/var/lib/mysql socket/var/lib/mysql/mysql.sock#Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links0log-error/var/log/mysqld.log pid-file/var/run/mysqld/mysqld.pid character-set-serverutf8default-storage-engineinnodb # 配置完毕重启mysql即可16. 常见问题mysql 已经配置了客⼾端服务器utf8编码但是⽆法输⼊中⽂ 确保您在终端命令⾏中可以输⼊中⽂[whbVM-0-3-centos~]$ env|grep LANG LANGen_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