redhat Linux6下搭建MySQL服务器

来源:互联网 发布:135端口怎么关闭 编辑:程序博客网 时间:2024/06/03 17:48

步骤1:安装MySQL。
首先搭建本地YUM源,然后挂载光盘镜像,再使用yum命令安装mysql包。

[root@localhost ~]# yum install -y mysql*  

步骤2:启动MySQL服务。

[root@localhost ~]# service mysqld start   

步骤3:设置数据库管理员密码。

[root@localhost ~]# mysqladmin -u root password text123    

步骤4:登录MySQL,创建数据库及表。

[root@localhost ~]# mysql -u root -p   # 登录MySQLEnter password:                         # 输入管理员密码Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.1.47 Source distributionCopyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.This software comes with ABSOLUTELY NO WARRANTY. This is free software,and you are welcome to modify and redistribute it under the GPL v2 licenseType 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>create database info;         # 创建名为hxinfo的数据库Query OK, 1 row affected (0.00 sec)     # 提示创建成功mysql> show databases;                  # 显示MySQL中当前的数据库+--------------------+| Database           |+--------------------+| information_schema || hxinfo             || mysql              || test               |+--------------------+4 rows in set (0.00 sec)mysql> use info                     # 使用新创建的数据库infoDatabase changedmysql> create table texttable(id int(10),name varchar(20),sex char(2),age int,primary key (id));        # 创建名为texttable的表,并设置id字段为主键Query OK, 0 rows affected (0.01 sec)    # 提示创建成功    mysql> show tables;                     # 显示当前数据库hxinfo中的表+------------------+| Tables_in_hxinfo |+------------------+| texttable          |+------------------+1 row in set (0.01 sec)mysql> describe texttable;              # 查看texttable表的相关字段信息+-------+-------------+------+-----+---------+-------+| Field | Type        | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id    | int(10)     | NO   | PRI | 0       |       |  # 可以看到id被标记为主键(PRI)| name  | varchar(20) | YES  |     | NULL    |       || sex   | char(2)     | YES  |     | NULL    |       || age   | int(11)     | YES  |     | NULL    |       |+-------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)mysql> insert into texttable values ('001','zhangsan','m','20');            # 插入一条信息Query OK, 1 row affected (0.02 sec)     # 提示操作成功mysql> select * from texttable;         # 查询texttable表的信息+----+----------+------+------+         # 可以查看到新插入的员工信息| id | name     | sex  | age  | +----+----------+------+------+|  1 | zhangsan | m    |   20 |+----+----------+------+------+1 row in set (0.00 sec) 

下面为操作截图



删除数据库和数据表,可以使用以下命令:

drop database [数据库名]            # 删除数据库drop table [数据表名]               # 删除数据表

注意:MySQL中的操作语句后面一定要加“;”号,除了use [数据库名]这个语句。

步骤5:创建两个用户webshow和webup,并赋予不同权限。

要求,用户webshow仅对hxinfo的数据库有select权限,用户webup对hxinfo的数据库有select、insert、update和delete权限,两个用户只允许在服务器本机上登录。用户webshow的密码为123456,用户webup的密码为654321。

# 以管理员身份登录MySQL,进行以下操作mysql> grant select on info. * to webshow@localhost identified by '123456';     # 创建用户webshowQuery OK, 0 rows affected (0.01 sec)    # 提示成功mysql> grant select,insert,update,delete on info. * to webup@localhost identified by '654321';  # 创建用户webupQuery OK, 0 rows affected (0.00 sec)    # 提示成功mysql> quit                             # 退出MySQLBye[root@localhost ~]# mysql -u webshow -p    # 使用webshow登录Enter password:                         # 输入webshow密码   mysql> use info                     # 使用数据库infomysql> select * from texttable;         # 查询texttable表+----+----------+------+------+         | id | name     | sex  | age  |         # 可以看到表中信息+----+----------+------+------+|  1 | zhangsan | m    |   20 |+----+----------+------+------+1 row in set (0.00 sec)mysql> insert into texttable values ('002','lisi','f','25');                # 新插入一条信息ERROR 1142 (42000): INSERT command denied to user 'webshow'@'localhost' for table 'texttable'   # 提示操作失败mysql> delete from texttable;               # 删除信息ERROR 1142 (42000): DELETE command denied to user 'webshow'@'localhost' for table 'texttable'   # 提示失败mysql> quit                             # 退出MySQLBye[root@localhost ~]# mysql -u webup -p  # 使用webup登录Enter password:                         # 输入webup密码mysql> use info                     # 使用数据库infomysql> select * from texttable;         # 查询texttable表+----+----------+------+------+| id | name     | sex  | age  |         # 可以看到表中的信息+----+----------+------+------+|  1 | zhangsan | m    |   20 |+----+----------+------+------+1 row in set (0.01 sec)mysql> insert into texttable values ('002','lisi','f','25');                # 新插入一条信息Query OK, 1 row affected (0.01 sec)     # 提示成功mysql> select * from texttable;         # 查询texttable表+----+----------+------+------+         # 可以看到新插入表中的信息| id | name     | sex  | age  |+----+----------+------+------+|  1 | zhangsan | m    |   20 ||  2 | lisi     | f    |   25 |+----+----------+------+------+2 rows in set (0.00 sec)mysql> delete from texttable where name='lisi'; # 删除texttable表中信息Query OK, 1 row affected (0.00 sec)     # 提示成功mysql> select * from texttable;     # 查询texttable表+----+----------+------+------+| id | name     | sex  | age  |     # 可以看到lisi信息已删除+----+----------+------+------+|  1 | zhangsan | m    |   20 |+----+----------+------+------+1 row in set (0.00 sec) 

以下为操作截图:



通过以上测试,可以看出这两个用户被赋予了不同的权限。

注意:若使用“delete from texttable;”语句删除信息,后面不使用“where”子句,将会把texttable表中的所有信息都删除。

用户可以被赋予相关权限,也可以被撤销授权。撤销用户webup的授权使用以下命令。

[root@localhost ~]# mysql -u root -p   # 以管理员身份登录MySQL Enter password: mysql>revoke insert on info. * from webup@localhost;        # 撤销用户webup的插入权限mysql> quitBye[root@localhost ~]# mysql -u webup -p  # 使用webup登录MySQLEnter password:mysql> use infomysql> insert into texttable values ('003','wangwu','m','23');      # 插入新的信息ERROR 1142 (42000): INSERT command denied to user 'webup'@'localhost' for table 'texttable' # 提示操作失败,该用户已不具有插入权限mysql> quitBye[root@localhost ~]# mysql -u root -p   # 再次以管理员身份登录MySQLEnter password:mysql> revoke all on info. * from webup@localhost;      # 撤销webup的所有权限Query OK, 0 rows affected (0.00 sec)    mysql> quitBye[root@localhost ~]# mysql -u webup -p  # 再次使用webup登录MySQLEnter password:mysql> use info     # 使用数据库infoERROR 1044 (42000): Access denied for user 'webup'@'localhost' to database 'hxinfo'     # 提示失败,说明该用户已无法进行任何数据库操作    
原创粉丝点击