6、CentOS 7 yum 安装 MySQL

添加 MySQL Yum Repository

MySQL 官方 Yum Repository

sudo yum localinstall https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
查看 MySQL 存储库是否添加成功
yum repolist enabled | grep "mysql.*-community.*"
选择 MySQL 的版本
yum repolist all | grep mysql # 查看所有版本
sudo yum-config-manager --disable mysql57-community # 禁用5.7的版本
sudo yum-config-manager --enable mysql80-community # 启用8.0的版本

# 还可以通过手动编辑/etc/yum.repos.d/mysql-community.repo 文件来选择

# 查看启用的版本
yum repolist enabled | grep mysql 
安装 MySQL
sudo yum install -y mysql-community-server
启动 MySQL 服务器
sudo systemctl enable mysqld # 开启自启
sudo systemctl disable mysqld # 关闭自启
sudo systemctl start mysqld # 启动服务
sudo systemctl status mysqld # 查看状态

sudo service mysqld start # 启动服务
sudo service mysqld status # 查看状态
MySQL 安全设置 (推荐第2种方式)
  • 通过日志查看默认的临时密码
# 安装完成,MySQL会默认创建 root 账户,密码保存在日志中,通过以下命令查看账户与密码
$ sudo grep 'temporary password' /var/log/mysqld.log
  1. 登录并修改默认密码
# 通过临时密码登录并修改root密码
$ mysql -uroot -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'w+daDp=ti40<1';
  1. 通过 mysql_secure_installation 命令重新设置密码
$ sudo mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root: # 输入临时密码

The existing password for the user account root has expired. Please set a new password.

New password:  # 输入新密码
Re-enter new password: # 再次输入

Estimated strength of the password: 100 # 密码强度评级
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 是否使用输入的密码?

By default, a MySQL installation has an anonymous user, # 默认情况下,MySQL有一个匿名用户
allowing anyone to log into MySQL without having to have # 这个匿名用户允许任何人登录到MySQL
a user account created for them. This is intended only for # 这只是为了方便测试使用
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production # 在正式环境使用的时候,建议你移除它
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 提示移除匿名用户
Success.

Normally, root should only be allowed to connect from # 一般情况下,root用户只允许使用"localhost"方式登录,
'localhost'. This ensures that someone cannot guess at
the root password from the network. # 以此确保,不能被某些人通过网络的方式访问

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : No # 不允许root远程登陆?

 ... skipping.
By default, MySQL comes with a database named 'test' that # 默认情况下,MySQL数据库中有一个 test database
anyone can access. This is also intended only for testing, # 这也仅仅是为了测试
and should be removed before moving into a production # 在正式环境下,应该移除掉
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y # 确认删除test数据库?
- Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately. # 刷新权限表,以确保所有的修改可以立刻生效

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :  y # 是否刷新?
Success.

All done!
工具无法连接 mysql 解决办法
# 查看所有用户权限
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;

# 添加root用户在所有ip可以登录
- 5.*版本
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
- 8.*版本
CREATE USER 'root1'@'%' IDENTIFIED BY 'w+daDp=ti40<1';
GRANT ALL PRIVILEGES ON *.* TO 'root1'@'%' WITH GRANT OPTION;

# 刷新权限,使修改生效
flush privileges;

# 工具需要先ssh登录,再登录MySQL
MySQL 官方安装介绍:

MySQL 安装

本作品采用《CC 协议》,转载必须注明作者和本文链接
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 文章来源blog.arunfung.com
arunfung
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!