Centos、Ubuntu系统下卸载、安装MySQL及用户的创建、授权和使用

由于经常使用linux系统,并且大数据环境搭建中经常会使用到mysql,不像windows系统下的安装,今天有点空写一篇,下面我给大家演示一遍。

主要有三部分内容:

1、MySQL的卸载

2、MySQL的安装

3、MySQL用户的授权和用户创建、删除

一、MySQL的卸载(如果是仅仅安装MySQL的用户可以跳过这一部分)

a)查看系统中以rpm包安装的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

b)卸载mysql

[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包

c)关闭mysql服务

[hadoop@master ~]$ systemctl stop mysql

d)删除分散mysql文件夹

whereis mysql 和sudo find / -name mysql

Centos、Ubuntu系统下卸载、安装MySQL及用户的创建、授权和使用_第1张图片

当然每个人的情况都不一样,但删除的方式是一样的、、哈哈哈 可以使用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应该已经完全卸载干净了

二、MySQL的安装

a) 下载mysql rpm安装包

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

 b) 安装libaio 需要libaio的依赖

sudo yum install libaio

c)rpm安装

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 

Centos、Ubuntu系统下卸载、安装MySQL及用户的创建、授权和使用_第2张图片

d)运行和配置mysql

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!

e) 启动MySQL

mysql -uroot -p

Centos、Ubuntu系统下卸载、安装MySQL及用户的创建、授权和使用_第3张图片

三、MySQL用户的授权和用户创建、删除

默认情况下制定在本地使用root的账号密码进行连接,远程连接不了,需要配置权限
#(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接)

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES; //刷新权限

此刻,就可以在其他地方远程连接mysql数据库了。由于我们经常会创建一个新用户来使用数据库

1) 查看所有用户的权限列表

mysql> show grants;

2)查看指定用户的权限信息

mysql> show grants for 'root'@'localhost';
mysql> show grants for 'root'@'%';

3)创建新用户(需要在root的方式登陆下创建)

命令:

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

说明:

  • username:你将创建的用户名
  • host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
  • 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'@'%';

4)授权(需要在root的方式登陆下创建)

命令:

说明:

  • privileges:用户的操作权限,如SELECTINSERTUPDATE等,如果要授予所的权限则使用ALL
  • databasename:数据库名
  • tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

例子:

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;

 5)设置与更改用户密码

命令:

SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

如果是当前登陆用户用:

SET PASSWORD = PASSWORD("newpassword");

例子:

SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");

6)撤销用户权限

命令:

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'@'%'; 查看。

7)删除用户

命令:

DROP USER 'username'@'host';

 

你可能感兴趣的:(数据库,技术分享,mysql,数据库,数据库用户,数据库权限)