CentOS 安装 MySQL
1. 下载安装包
Section titled “1. 下载安装包”1.1. 链接
Section titled “1.1. 链接”https://dev.mysql.com/downloads/mysql/
1.2. 版本选择
Section titled “1.2. 版本选择”- OS : Red Hat Enterprise Linux 7 / Oracle Linux
- Version : Red Hat Enterprise Linux 7 / Oracle Linux (x86, 64-bit)
- RPM Bundle - Download
- No thanks, just start my download.
1.3. 上传 tar 文件至虚拟机
Section titled “1.3. 上传 tar 文件至虚拟机”移动到 /usr/local/mysql/ 目录下。此步骤需要在 /usr/local/ 下新建 mysql 文件夹。
sudo mv mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar /usr/local/mysql/2. 删除mariadb
Section titled “2. 删除mariadb”2.1. 通过命令查看 mariadb 的安装包
Section titled “2.1. 通过命令查看 mariadb 的安装包”rpm -qa | grep mariadb2.2. 通过命令卸载 mariadb
Section titled “2.2. 通过命令卸载 mariadb”rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps通过命令查看 mariadb 是否卸载
rpm -qa | grep mariadb3. 安装 MySQL
Section titled “3. 安装 MySQL”pwd/usr/local/mysql3.1. 解压
Section titled “3.1. 解压”sudo tar -xvf mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar3.2. 安装 common
Section titled “3.2. 安装 common”sudo rpm -ivh mysql-community-common-8.0.27-1.el7.x86_64.rpm --nodeps --force3.3. 安装 libs
Section titled “3.3. 安装 libs”sudo rpm -ivh mysql-community-libs-8.0.27-1.el7.x86_64.rpm --nodeps --force3.4. 安装 client
Section titled “3.4. 安装 client”sudo rpm -ivh mysql-community-client-8.0.27-1.el7.x86_64.rpm --nodeps --force3.5. 安装 server
Section titled “3.5. 安装 server”该步骤花费时间较长
sudo rpm -ivh mysql-community-server-8.0.27-1.el7.x86_64.rpm --nodeps --force3.6. 通过命令查看安装包
Section titled “3.6. 通过命令查看安装包”rpm -qa | grep mysql4. 初始化与相关配置
Section titled “4. 初始化与相关配置”4.1. 初始化
Section titled “4.1. 初始化”sudo mysqld --initialize;如果遇到报错 mysqld: error while loading shared libraries: libnuma.so.1,执行下面的命令
yum -y install numactlsudo chown mysql:mysql /var/lib/mysql -R;sudo systemctl start mysqld.service;sudo systemctl enable mysqld;4.2. 查看密码
Section titled “4.2. 查看密码”sudo cat /var/log/mysqld.log | grep password2021-11-21T14:21:26.120237Z 6 [Note] [MY-010454] [Server]A temporary password is generated for root@localhost: 3l-/wkz#>iHl5. 使用数据库
Section titled “5. 使用数据库”5.1. 登录
Section titled “5.1. 登录”mysql -uroot -p密码即为刚刚生成的字符串。
5.2. 退出
Section titled “5.2. 退出”exit;6. Access denied
Section titled “6. Access denied”6.1. 编辑 /etc/my.cnf
Section titled “6.1. 编辑 /etc/my.cnf”在文件尾部添加以下内容
skip-grant-tables6.2. 重启 mysql 服务
Section titled “6.2. 重启 mysql 服务”sudo service mysqld restart6.3. 再次登录 mysql
Section titled “6.3. 再次登录 mysql”mysql -uroot -p此时登录密码为空。
6.4. 修改密码
Section titled “6.4. 修改密码”6.4.1. 清空 authentication_string
Section titled “6.4.1. 清空 authentication_string”use mysql;UPDATE user SET authentication_string = '' WHERE User = 'root';6.4.2. 关闭 skip-grant-tables
Section titled “6.4.2. 关闭 skip-grant-tables”- 退出数据库
- 删除
/etc/my.cnf的skip-grant-tables - 重启 mysql 服务
6.4.3. 真·修改密码
Section titled “6.4.3. 真·修改密码”无密码登录 mysql,并通过下面的脚本命令修改密码。
ALTER user 'root'@'localhost' IDENTIFIED BY 'a123456'; -- BY 后面为密码6.4.4. 重新登录数据库
Section titled “6.4.4. 重新登录数据库”可以使用刚刚的密码登录
7. 开启外部访问
Section titled “7. 开启外部访问”7.1. 登录 MySQL
Section titled “7.1. 登录 MySQL”7.2. 进入 mysql 库
Section titled “7.2. 进入 mysql 库”use mysql;7.3. 更新域属性,'%' 表示允许外部访问:
Section titled “7.3. 更新域属性,'%' 表示允许外部访问:”update user set host='%' where user ='root';7.4. flush
Section titled “7.4. flush”FLUSH PRIVILEGES;7.5. 再执行授权语句
Section titled “7.5. 再执行授权语句”GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;7.6. 测试
Section titled “7.6. 测试”TODO