一.MYSQL在Centos7下的安装注意安装与卸载中⽤⼾全部切换成为root初期练习mysql不进⾏⽤⼾管理全部使⽤root进⾏1.卸载内置环境1-1卸载不要的环境[rootVM-0-3-centos ~]$ ps ajx |grep mariadb # 先检查是否有mariadb存在 13134 14844 14843 13134 pts/0 14843 S 1005 0:00 grep --colorauto 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 [rootVM-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 [rootVM-0-3-centos ~]$ ps axj |grep mariadb# 停⽌完成 13134 14976 14975 13134 pts/0 14975 S 1005 0:00 grep --colorauto mariadbps axj | grep mariadb查看进程systemctl stop mariadb停止服务MariaDB 是 MySQL 的开源分支可保留可卸载根据实际需求决定。1-2检查系统安装包[rootVM-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_641-3卸载这些默认安装包#卸载显⽰出来的mariadb/mysql安装包 [roootVM-0-3-centos ~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧 备份/etc/my.cnf,备份/var/lib/mysql数据1-4获取mysql官⽅yum源#1. 获取mysql官⽅yum源 http://repo.mysql.com/ # ⼀定要看下⾯的⽂字如果后⾯⾃⼰安装不成功就看看这个 # 注意最好安装和⾃⼰系统⼀致的mysql版本否则可能会存在软件兼容性问题 # 查看⾃⼰的系统版本 # [rootVM-0-3-centos ~]# cat /etc/redhat-release # CentOS Linux release 7.8.2003 (Core) # 可以进⼊ http://repo.mysql.com/ 找⼀下和⾃⼰版本⼀致的资源 # 下载到你的本地然后上传到你的Linux服务器下载 MySQL的 yum源包wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm2.安装mysql yum 源# 安装前 [rootVM-0-3-centos 9.9]$ ls mysql57-community-release-el7-10.noarch.rpm 查看你本地的yum源 [我的和你的肯定不同但是没有mysql相关的yum源] [rootVM-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.rep -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看看能不能正常⼯作[rootVM-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 base3.进行安装3-1安装mysql服务[rootVM-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 #服务器 ... #安装了服务和客⼾端3-2查看配置⽂件和数据存储位置[rootVM-0-3-centos 9.9]$ ls /etc/my.cnf /etc/my.cnf [rootVM-0-3-centos 9.9]$ sudo ls /var/lib/mysql ....3-3启动服务[rootVM-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 3-4查看启动服务[rootVM-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 --colorauto mysqld4.进行登录4-1登陆⽅法⼀#获取临时root密码 [rootVM-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 rootlocalhost: yLMalT:v5l* #使⽤临时密码登录 [rootVM-0-3-centos 9.9]$ mysql -uroot -p Enter password: #判断修改密码时候新密码是否符合当前的策略不满⾜报错不让修改关闭它 #安全强度默认为中即1要求必须包含 数字、符号、⼤⼩写字⺟⻓度⾄少为8位 mysql set global validate_password_policy0; Query OK, 0 rows affected (0.00 sec) #密码最⼩⻓度 mysql set global validate_password_length1; Query OK, 0 rows affected (0.00 sec) #修改本地登录密码暂不授权远程登录 mysql ALTER USER rootlocalhost IDENTIFIED BY qwerwu.888; Query OK, 0 rows affected (0.00 sec) mysql FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) #如果你安装的最新的mysql没有所谓的临时密码root默认没有密码4-2 登陆⽅法⼆# 如果你安装的最新的mysql没有所谓的临时密码root默认没有密码 # 试着直接client登陆⼀下4-3 登陆⽅式三[rootVM-0-3-centos 9.9]# vim /etc/my.cnf # 打开mysql配置⽂件 在[mysqld]最后⼀栏配置(不知道是什么就放在配置⽂件最后) 加⼊: skip-grant-tables 选项, 并保存退出 [rootVM-0-3-centos 9.9]# systemctl restart mysqld # 重启mysql服务 # 登陆成功 rootVM-0-3-centos 9.9]# 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. mysql5.设置配置文件5-1设置开机启动【可以不设】得先退出mysql在进行#开启开机⾃启动 systemctl enable mysqld systemctl daemon-reload5-2配置my.cnf#配置一下my.conf,主要是数据库客户端和服务器的编码格式 [rootVM-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 [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 port3306 character-set-serverutf8 default-storage-engineinnodb datadir/var/lib/mysql socket/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links0 character-set-serverutf8 default-storage-engineinnodb log-error/var/log/mysqld.log pid-file/var/run/mysqld/mysqld.pid skip-grant-tables 配置完毕重启即可设置 MySQL 的字符集为 UTF-8令其支持中文vim /etc/my.cnf重启一下 MySQL令配置生效重启 mysqlservice mysqld restartor停止 mysqlservice mysqld stop启动 mysqlservice mysqld start5-3常⻅问题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二..MYSQL在ubuntu(22.04)下的安装Ubuntu 20.04 安装 MySQL 步骤纯步骤1. 更新软件源sudo apt update sudo apt upgrade -y2. 安装 MySQL 服务器sudo apt install mysql-server -y3. 启动服务并设置开机自启sudo systemctl start mysql sudo systemctl enable mysql4. 运行安全初始化脚本sudo mysql_secure_installation 按提示设置密码、移除匿名用户、禁止root远程登录等5. 登录 MySQL默认sudo方式sudo mysql -u rootALTER USER rootlocalhost IDENTIFIED WITH mysql_native_password BY 你设置的密码;一旦你执行这个命令就不花再有免密登录这一说相当于你强制mysql进行密码登录6. 修改root为密码登录ALTER USER rootlocalhost IDENTIFIED WITH mysql_native_password BY 你的密码; FLUSH PRIVILEGES; exit;7. 可选用密码登录验证mysql -u root -p三.CentOS7 VS Ubuntu20.04 MySQL安装完整区别1. 包管理器CentOS7yum必须手动下载官方源Ubuntu20.04apt系统自带源不用配源直接装2. 安装命令CentOS7 yum install mysql-community-serverUbuntu20.04 apt install mysql-server3. 初始登录方式核心区别CentOS7自带临时初始密码直接 mysql -u root -p 登录Ubuntu20.04无密码只能sudo免密登录必须手动改密码4. 配置文件路径CentOS7 /etc/my.cnfUbuntu20.04 /etc/mysql/mysql.conf.d/mysqld.cnf5. 系统自带冲突库CentOS7自带MariaDB必须先卸载Ubuntu20.04无冲突库直接安装6. 可选MySQL版本CentOS7可自由装 5.7 / 8.0Ubuntu20.04官方源只能装8.0装不了5.77. 防火墙端口CentOS7默认防火墙开启3306端口默认放行Ubuntu20.04默认ufw防火墙关闭3306端口不放行8. 安全脚本CentOS7安装后必须手动执行 mysql_secure_installationUbuntu20.04推荐执行但不强制不改密码也能sudo登录四.解释为什么 CentOS 7 和 Ubuntu 22.04 安装 MySQL 差距这么大同样是安装 MySQLUbuntu 22.04 一条命令就能搞定CentOS 7 却要手动加源、卸载冲突包、配置一堆东西为什么两者差异这么大一、核心根源发行版的软件源策略不同1. Ubuntu 22.04默认源直接集成 MySQLUbuntu 属于 Debian 系 发行版它的官方 apt 软件源中默认就包含了 MySQL 8.0 的官方适配包 mysql-server 。你只需要执行 sudo apt update sudo apt install mysql-server 就能直接完成安装不需要手动添加第三方源。系统会自动处理所有依赖甚至帮你完成初始化、启动服务、设置开机自启。同时 Ubuntu 对 MySQL 做了定制化适配比如默认使用 auth_socket 认证让 sudo mysql 可以免密登录进一步降低了上手门槛。2. CentOS 7默认源只有 MariaDB没有官方 MySQLCentOS 属于 RHEL 系 发行版在 MySQL 被 Oracle 收购后CentOS 7 的官方 yum 源彻底移除了官方 MySQL 包默认只提供 MariaDBMySQL 的开源分支。如果你直接在 CentOS 7 执行 yum install mysql-server 要么找不到包要么会自动安装 MariaDB而不是你想要的官方 MySQL。想要安装官方 MySQL必须手动下载并添加 MySQL 官方 YUM 源还要先卸载系统自带的 MariaDB 相关包避免端口、配置冲突才能正常安装。整个过程需要手动处理源、依赖、冲突步骤自然比 Ubuntu 繁琐很多。二、包管理器的生态差异1. apt Ubuntu开箱即用的丰富生态Ubuntu 的 apt 包管理器软件源覆盖范围极广对常用开源软件比如 MySQL、Nginx、PHP的支持非常完善很多工具都默认集成在官方源中用户几乎不需要手动配置源。同时 apt 的依赖解析能力更强安装时会自动补全所有需要的环境、库文件用户不需要额外安装依赖。2. yum CentOS 7保守的企业级源CentOS 7 定位是企业级服务器系统它的 yum 源以稳定、保守为核心原则只保留经过严格测试的系统级软件不会主动集成第三方商业软件比如 Oracle 旗下的 MySQL。企业级系统更倾向于使用开源分支 MariaDB 来替代 MySQL因此官方源不会维护 MySQL 包。yum 对依赖的处理相对严格手动加源后还需要手动导入 GPG 密钥、处理依赖冲突进一步增加了操作步骤。3.默认认证方式的差异遇到的免密问题除了安装步骤两者的默认登录认证逻辑也完全不同这也是很多同学觉得「差距大」的关键Ubuntu 22.04默认使用 auth_socket 认证只要你是 Linux 的 root 用户通过 sudo 提权就可以直接用 sudo mysql 免密登录 MySQL不需要输入数据库密码对新手非常友好。CentOS 7默认使用 mysql_native_password 密码认证安装时会生成临时初始密码必须用 mysql -u root -p 输入密码才能登录没有「免密」一说安全性更高但上手更复杂。4.总结为什么 Ubuntu 更省心维度 Ubuntu 22.04 CentOS 7软件源 默认集成 MySQL 8.0直接安装 默认只有 MariaDB需手动加 MySQL 源包管理器 apt 生态丰富自动处理依赖 yum 源保守需手动处理依赖/冲突认证方式 auth_socket 免密登录新手友好 mysql_native_password 密码登录更安全安装步骤 1 条命令完成 卸载 MariaDB → 加源 → 安装 → 改密码简单来说Ubuntu 是桌面级易用性优先CentOS 7 是企业级稳定优先两者的定位不同直接导致了 MySQL 安装体验的巨大差异。补充CentOS 7 也能像 Ubuntu 一样一键安装吗可以但需要手动配置1. 卸载 MariaDB sudo yum remove -y mariadb-libs2. 添加 MySQL 官方 YUM 源 sudo yum localinstall -y mysql80-community-release-el7-7.noarch.rpm3. 安装 MySQL sudo yum install -y mysql-community-server4. 启动服务并初始化 sudo systemctl start mysqld sudo systemctl enable mysqld只是这个过程需要手动操作不像 Ubuntu 那样开箱即用。
【MYSQL】在Centos7和ubuntu22.04环境下安装
发布时间:2026/5/18 16:11:38
一.MYSQL在Centos7下的安装注意安装与卸载中⽤⼾全部切换成为root初期练习mysql不进⾏⽤⼾管理全部使⽤root进⾏1.卸载内置环境1-1卸载不要的环境[rootVM-0-3-centos ~]$ ps ajx |grep mariadb # 先检查是否有mariadb存在 13134 14844 14843 13134 pts/0 14843 S 1005 0:00 grep --colorauto 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 [rootVM-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 [rootVM-0-3-centos ~]$ ps axj |grep mariadb# 停⽌完成 13134 14976 14975 13134 pts/0 14975 S 1005 0:00 grep --colorauto mariadbps axj | grep mariadb查看进程systemctl stop mariadb停止服务MariaDB 是 MySQL 的开源分支可保留可卸载根据实际需求决定。1-2检查系统安装包[rootVM-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_641-3卸载这些默认安装包#卸载显⽰出来的mariadb/mysql安装包 [roootVM-0-3-centos ~]$ sudo yum remove mariadb # 还是试着将上⾯的包都⼀个⼀个卸载吧 备份/etc/my.cnf,备份/var/lib/mysql数据1-4获取mysql官⽅yum源#1. 获取mysql官⽅yum源 http://repo.mysql.com/ # ⼀定要看下⾯的⽂字如果后⾯⾃⼰安装不成功就看看这个 # 注意最好安装和⾃⼰系统⼀致的mysql版本否则可能会存在软件兼容性问题 # 查看⾃⼰的系统版本 # [rootVM-0-3-centos ~]# cat /etc/redhat-release # CentOS Linux release 7.8.2003 (Core) # 可以进⼊ http://repo.mysql.com/ 找⼀下和⾃⼰版本⼀致的资源 # 下载到你的本地然后上传到你的Linux服务器下载 MySQL的 yum源包wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm2.安装mysql yum 源# 安装前 [rootVM-0-3-centos 9.9]$ ls mysql57-community-release-el7-10.noarch.rpm 查看你本地的yum源 [我的和你的肯定不同但是没有mysql相关的yum源] [rootVM-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.rep -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看看能不能正常⼯作[rootVM-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 base3.进行安装3-1安装mysql服务[rootVM-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 #服务器 ... #安装了服务和客⼾端3-2查看配置⽂件和数据存储位置[rootVM-0-3-centos 9.9]$ ls /etc/my.cnf /etc/my.cnf [rootVM-0-3-centos 9.9]$ sudo ls /var/lib/mysql ....3-3启动服务[rootVM-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 3-4查看启动服务[rootVM-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 --colorauto mysqld4.进行登录4-1登陆⽅法⼀#获取临时root密码 [rootVM-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 rootlocalhost: yLMalT:v5l* #使⽤临时密码登录 [rootVM-0-3-centos 9.9]$ mysql -uroot -p Enter password: #判断修改密码时候新密码是否符合当前的策略不满⾜报错不让修改关闭它 #安全强度默认为中即1要求必须包含 数字、符号、⼤⼩写字⺟⻓度⾄少为8位 mysql set global validate_password_policy0; Query OK, 0 rows affected (0.00 sec) #密码最⼩⻓度 mysql set global validate_password_length1; Query OK, 0 rows affected (0.00 sec) #修改本地登录密码暂不授权远程登录 mysql ALTER USER rootlocalhost IDENTIFIED BY qwerwu.888; Query OK, 0 rows affected (0.00 sec) mysql FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) #如果你安装的最新的mysql没有所谓的临时密码root默认没有密码4-2 登陆⽅法⼆# 如果你安装的最新的mysql没有所谓的临时密码root默认没有密码 # 试着直接client登陆⼀下4-3 登陆⽅式三[rootVM-0-3-centos 9.9]# vim /etc/my.cnf # 打开mysql配置⽂件 在[mysqld]最后⼀栏配置(不知道是什么就放在配置⽂件最后) 加⼊: skip-grant-tables 选项, 并保存退出 [rootVM-0-3-centos 9.9]# systemctl restart mysqld # 重启mysql服务 # 登陆成功 rootVM-0-3-centos 9.9]# 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. mysql5.设置配置文件5-1设置开机启动【可以不设】得先退出mysql在进行#开启开机⾃启动 systemctl enable mysqld systemctl daemon-reload5-2配置my.cnf#配置一下my.conf,主要是数据库客户端和服务器的编码格式 [rootVM-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 [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 port3306 character-set-serverutf8 default-storage-engineinnodb datadir/var/lib/mysql socket/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links0 character-set-serverutf8 default-storage-engineinnodb log-error/var/log/mysqld.log pid-file/var/run/mysqld/mysqld.pid skip-grant-tables 配置完毕重启即可设置 MySQL 的字符集为 UTF-8令其支持中文vim /etc/my.cnf重启一下 MySQL令配置生效重启 mysqlservice mysqld restartor停止 mysqlservice mysqld stop启动 mysqlservice mysqld start5-3常⻅问题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二..MYSQL在ubuntu(22.04)下的安装Ubuntu 20.04 安装 MySQL 步骤纯步骤1. 更新软件源sudo apt update sudo apt upgrade -y2. 安装 MySQL 服务器sudo apt install mysql-server -y3. 启动服务并设置开机自启sudo systemctl start mysql sudo systemctl enable mysql4. 运行安全初始化脚本sudo mysql_secure_installation 按提示设置密码、移除匿名用户、禁止root远程登录等5. 登录 MySQL默认sudo方式sudo mysql -u rootALTER USER rootlocalhost IDENTIFIED WITH mysql_native_password BY 你设置的密码;一旦你执行这个命令就不花再有免密登录这一说相当于你强制mysql进行密码登录6. 修改root为密码登录ALTER USER rootlocalhost IDENTIFIED WITH mysql_native_password BY 你的密码; FLUSH PRIVILEGES; exit;7. 可选用密码登录验证mysql -u root -p三.CentOS7 VS Ubuntu20.04 MySQL安装完整区别1. 包管理器CentOS7yum必须手动下载官方源Ubuntu20.04apt系统自带源不用配源直接装2. 安装命令CentOS7 yum install mysql-community-serverUbuntu20.04 apt install mysql-server3. 初始登录方式核心区别CentOS7自带临时初始密码直接 mysql -u root -p 登录Ubuntu20.04无密码只能sudo免密登录必须手动改密码4. 配置文件路径CentOS7 /etc/my.cnfUbuntu20.04 /etc/mysql/mysql.conf.d/mysqld.cnf5. 系统自带冲突库CentOS7自带MariaDB必须先卸载Ubuntu20.04无冲突库直接安装6. 可选MySQL版本CentOS7可自由装 5.7 / 8.0Ubuntu20.04官方源只能装8.0装不了5.77. 防火墙端口CentOS7默认防火墙开启3306端口默认放行Ubuntu20.04默认ufw防火墙关闭3306端口不放行8. 安全脚本CentOS7安装后必须手动执行 mysql_secure_installationUbuntu20.04推荐执行但不强制不改密码也能sudo登录四.解释为什么 CentOS 7 和 Ubuntu 22.04 安装 MySQL 差距这么大同样是安装 MySQLUbuntu 22.04 一条命令就能搞定CentOS 7 却要手动加源、卸载冲突包、配置一堆东西为什么两者差异这么大一、核心根源发行版的软件源策略不同1. Ubuntu 22.04默认源直接集成 MySQLUbuntu 属于 Debian 系 发行版它的官方 apt 软件源中默认就包含了 MySQL 8.0 的官方适配包 mysql-server 。你只需要执行 sudo apt update sudo apt install mysql-server 就能直接完成安装不需要手动添加第三方源。系统会自动处理所有依赖甚至帮你完成初始化、启动服务、设置开机自启。同时 Ubuntu 对 MySQL 做了定制化适配比如默认使用 auth_socket 认证让 sudo mysql 可以免密登录进一步降低了上手门槛。2. CentOS 7默认源只有 MariaDB没有官方 MySQLCentOS 属于 RHEL 系 发行版在 MySQL 被 Oracle 收购后CentOS 7 的官方 yum 源彻底移除了官方 MySQL 包默认只提供 MariaDBMySQL 的开源分支。如果你直接在 CentOS 7 执行 yum install mysql-server 要么找不到包要么会自动安装 MariaDB而不是你想要的官方 MySQL。想要安装官方 MySQL必须手动下载并添加 MySQL 官方 YUM 源还要先卸载系统自带的 MariaDB 相关包避免端口、配置冲突才能正常安装。整个过程需要手动处理源、依赖、冲突步骤自然比 Ubuntu 繁琐很多。二、包管理器的生态差异1. apt Ubuntu开箱即用的丰富生态Ubuntu 的 apt 包管理器软件源覆盖范围极广对常用开源软件比如 MySQL、Nginx、PHP的支持非常完善很多工具都默认集成在官方源中用户几乎不需要手动配置源。同时 apt 的依赖解析能力更强安装时会自动补全所有需要的环境、库文件用户不需要额外安装依赖。2. yum CentOS 7保守的企业级源CentOS 7 定位是企业级服务器系统它的 yum 源以稳定、保守为核心原则只保留经过严格测试的系统级软件不会主动集成第三方商业软件比如 Oracle 旗下的 MySQL。企业级系统更倾向于使用开源分支 MariaDB 来替代 MySQL因此官方源不会维护 MySQL 包。yum 对依赖的处理相对严格手动加源后还需要手动导入 GPG 密钥、处理依赖冲突进一步增加了操作步骤。3.默认认证方式的差异遇到的免密问题除了安装步骤两者的默认登录认证逻辑也完全不同这也是很多同学觉得「差距大」的关键Ubuntu 22.04默认使用 auth_socket 认证只要你是 Linux 的 root 用户通过 sudo 提权就可以直接用 sudo mysql 免密登录 MySQL不需要输入数据库密码对新手非常友好。CentOS 7默认使用 mysql_native_password 密码认证安装时会生成临时初始密码必须用 mysql -u root -p 输入密码才能登录没有「免密」一说安全性更高但上手更复杂。4.总结为什么 Ubuntu 更省心维度 Ubuntu 22.04 CentOS 7软件源 默认集成 MySQL 8.0直接安装 默认只有 MariaDB需手动加 MySQL 源包管理器 apt 生态丰富自动处理依赖 yum 源保守需手动处理依赖/冲突认证方式 auth_socket 免密登录新手友好 mysql_native_password 密码登录更安全安装步骤 1 条命令完成 卸载 MariaDB → 加源 → 安装 → 改密码简单来说Ubuntu 是桌面级易用性优先CentOS 7 是企业级稳定优先两者的定位不同直接导致了 MySQL 安装体验的巨大差异。补充CentOS 7 也能像 Ubuntu 一样一键安装吗可以但需要手动配置1. 卸载 MariaDB sudo yum remove -y mariadb-libs2. 添加 MySQL 官方 YUM 源 sudo yum localinstall -y mysql80-community-release-el7-7.noarch.rpm3. 安装 MySQL sudo yum install -y mysql-community-server4. 启动服务并初始化 sudo systemctl start mysqld sudo systemctl enable mysqld只是这个过程需要手动操作不像 Ubuntu 那样开箱即用。