mysql安装
以5.7版本为例:
#创建mysql用户
useradd mysql
passwd mysql
# 分配sudo权限
usermod -aG wheel mysql
# 安装mysql
su mysql
sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm
sudo yum update
sudo yum install mysql-server
sudo systemctl start mysqld # 启动mysql
sudo systemctl status mysqld # 查看mysql进程状态
sudo grep 'temporary password' /var/log/mysqld.log #查看首次安装得到的随机密码
安全设置
使用mysql_secure_installation
命令进行安全设置,加固内容如下:
- 设置root密码
- 去掉匿名用户
- 删除test数据库
- 禁止root用户远程登录
示例:
[mysql@localhost ~]$ sudo mysql_secure_installation
[sudo] password for mysql:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Please set the password for root here.
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,
allowing anyone to log into MySQL without having to have
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
'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) : y
Success.
By default, MySQL comes with a database named 'test' that
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
- 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@localhost ~]$
安装问题
- 执行
sudo mysql_secure_installation
提示如下错误:
Failed! Error: Column count of mysql.user is wrong. Expected 45, found 43. Created with MySQL 50648, now running 50730. Please use mysql_upgrade to fix this error.
修复方式: mysql_upgrade --force -uroot -p
- 重置root密码
shell>sudo systemctl stop mysqld
shell>mysqld --skip-grant-tables --skip-networking &
shell>mysql
mysql>FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
mysql>exit
shell>fg
CRTL + C # 终止程序
shell>sudo systemctl start mysqld
卸载
sudo yum remove `rpm -aq | grep mysql`