主要有三部分内容:
1、MySQL的卸载
2、MySQL的安装
3、MySQL用户的授权和用户创建、删除
[hadoop@master ~]$ rpm -qa | grep -i mysql
MySQL-client-5.5.59-1.el7.x86_64
MySQL-server-5.5.59-1.el7.x86_64
[hadoop@master ~]$ sudo rpm -e MySQL-client-5.5.59-1.el7.x86_64
[hadoop@master ~]$ sudo rpm -e MySQL-server-5.5.59-1.el7.x86_64
现在可以使用rpm -qa | grep -i mysql来查看系统是否还有rpm安装的mysql包
[hadoop@master ~]$ systemctl stop mysql
whereis mysql 和sudo find / -name mysql
当然每个人的情况都不一样,但删除的方式是一样的、、哈哈哈 可以使用rm -rf 删除上面查找的mysql文件夹
清空相关mysql的所有目录以及文件,可以使用我的方式删除
sudo rm -rf /var/lib/mysql /usr/lib64/mysql /var/lib/mysql/mysql /usr/lib64/mysql
sudo rm -rf /usr/lib/mysql /usr/share/mysql /usr/my.cnf
通过以上几步,mysql应该已经完全卸载干净了
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
MySQL-server-5.5.59-1.el7.x86_64.rpm
MySQL-client-5.5.59-1.el7.x86_64.rpm
sudo yum install libaio
sudo rpm -ivh MySQL-server-5.5.59-1.el7.x86_64.rpm
sudo rpm -ivh MySQL-client-5.5.59-1.el7.x86_64.rpm
systemctl start mysql 启动mysql服务
安装完mysql-server 会提示可以运行mysql_secure_installation。运行mysql_secure_installation会执行几个设置: a)为root用户设置密码 b)删除匿名账号 c)取消root用户远程登录 d)删除test库和对test库的访问权限 e)刷新授权表使修改生效 通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,详细步骤请参看下面的命令: 复制代码 代码如下:
[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):<–初次运行直接回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
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? [Y/n] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
… 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? [Y/n] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… 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? [Y/n] <– 是否删除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? [Y/n] <– 是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
mysql -uroot -p
默认情况下制定在本地使用root的账号密码进行连接,远程连接不了,需要配置权限
#(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES; //刷新权限
此刻,就可以在其他地方远程连接mysql数据库了。由于我们经常会创建一个新用户来使用数据库
mysql> show grants;
mysql> show grants for 'root'@'localhost';
mysql> show grants for 'root'@'%';
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
%
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';
SELECT
,INSERT
,UPDATE
等,如果要授予所的权限则使用ALL
*
表示,如*.*
GRANT SELECT, INSERT ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';
GRANT ALL ON maindataplus.* TO 'pig'@'%';
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是当前登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
privilege, databasename, tablename:同授权部分
REVOKE SELECT ON *.* FROM 'pig'@'%';
假如你在给用户'pig'@'%'
授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'pig'@'%'
,则在使用REVOKE SELECT ON *.* FROM 'pig'@'%';
命令并不能撤销该用户对test数据库中user表的SELECT
操作。相反,如果授权使用的是GRANT SELECT ON *.* TO 'pig'@'%';
则REVOKE SELECT ON test.user FROM 'pig'@'%';
命令也不能撤销该用户对test数据库中user表的Select
权限。
具体信息可以用命令SHOW GRANTS FOR 'pig'@'%';
查看。
DROP USER 'username'@'host';