mysql的安装配置管理

来源:互联网 发布:淘宝上卖刀的正品店铺 编辑:程序博客网 时间:2024/05/08 23:09

1. 安装

如果我们在没有安装mysql的ubuntu系统中输入如下的命令:mysql, 系统会给出如下的提示:

The program 'mysql' is currently not installed.  You can install it by typing:sudo apt-get install mysql-client-core-5.5

实际上我们安装mysql的时候,可以选择安装server或者client,如果只是安装有client端的话,要连接到安装有server的服务器端才能访问。client提供了和sql脚本交互以及连接某个特定mysql服务器的功能。我们因为需要安装server,使用如下的命令就可以,而且同时也将client也包含安装在内了。

sudo apt-get install mysql-server  

或者

 yum -y install mysql-server


安装过程中会提示输入root帐号的密码。root是mysql系统中的管理员角色,具有最高的权限。

输入密码之后会提示再次确认密码,如下图:


确认完密码之后系统会自动完成后续的安装过程。

由于ubuntu上安装的mysql默认只允许本机连接,所以我们要修改下配置文件

 vim /etc/mysql/my.cnf
将文件中
bind-address        = 127.0.0.1 
注释掉
#bind-address       = 127.0.0.1

保存退出

然后重启mysql设置才能生效

/etc/init.d/mysql restart

2. 采用root登录 

安装过程之后,我们就可以登录进入mysql server了。mysql 默认的情况下是不允许匿名登录的。如果我们输入如下的命令:mysql,可能会得到如下的错误信息:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

既然我们前面指定了root用户的密码,可以采用如下的命令来登录:
mysql -u root -p  

-u 选项用来指定登录的用户,后面的root表示采用root帐号登录。-p选项表示采用密码验证的方式登录。

在输入前面的命令后,系统会提示输入密码,如果正确的话就可以进入系统了。



3. 创建新用户

在大多数情况下,如果我们将结合mysql进行一些开发工作的话,不会直接采用root账户。一般root账户用来做一些系统管理和维护的工作,而且因为root权限太高。如果mysql系统出现问题容易导致所有数据的破坏。所以我们需要专门创建一个特定的用户,由root来给它指派一定的权限。这样,就算该账户出现问题,造成的损失也可能只是该帐号权限范围内的,不至于整体的破坏。

比如说我们要创建一个新的用户,并设置该用户的访问密码,在以root用户登录进入系统后,执行如下命令:

insert into mysql.user(Host,User,Password)  values("localhost","firesnow",password("1234"));  

这样我们就添加了一个用户名为firesnow密码为1234的用户,但是这个账户目前还不能登陆,因为他没有任何权限。

删除用户

mysql>DELETE FROM mysql.user WHERE User="firesnow" and Host="localhost";  

修改用户密码

mysql>update mysql.user set password=password('123456') where User="firesnow" and Host="localhost";  


4. 配置新用户权限


配置用户权限我们要用到grant命令,他的格式可以简单概括为

grant 权限 on 数据库对象 to 用户@用户登陆地址

一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@'%'grant insert on testdb.* to common_user@'%'grant update on testdb.* to common_user@'%'grant delete on testdb.* to common_user@'%'

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@'%'

二、grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。

grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* to developer@'192.168.0.%';  grant alter on testdb.* to developer@'192.168.0.%';  grant drop on testdb.* to developer@'192.168.0.%'; 

MySQLgrant 操作 MySQL 外键权限。
grant references on testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to

grant 操作 MySQL 索引权限。
grant index on testdb.* to

grant 操作 MySQL 视图、查看视图源代码 权限。
grant create view on testdb.* to developer@'192.168.0.%';  grant show view on testdb.* to developer@'192.168.0.%'; 

grant 操作 MySQL 存储过程、函数 权限。
grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status  grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure  grant execute on testdb.* to developer@'192.168.0.%';

三、grant 普通 DBA 管理某个 MySQL 数据库的权限。


grant all privileges on testdb to

其中,关键字 “privileges” 可以省略。
grant execute on procedure testdb.pr_add to 'dba'@'localhost'  grant execute on function testdb.fn_add to 'dba'@'localhost'  grant all on *.* to dba@'localhost' 

四、MySQL grant 权限,分别可以作用在多个层次上。

1. grant 作用在整个 MySQL 服务器上:

可以查询 MySQL 中所有数据库中的表。
grant select on *.* to dba@localhost; 

可以管理 MySQL 中的所有数据库
grant all on *.* to dba@localhost; 


2. grant 作用在单个数据库上:

可以查询 testdb 中的表。
grant select on testdb.* to dba@localhost; 

3. grant 作用在单个数据表上:

grant select, insert, update, delete on testdb.orders to

4. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to

5.MySQL grant 作用在存储过程、函数上:

grant execute on procedure testdb.pr_add to 'dba'@'localhost'  grant execute on function testdb.fn_add to 'dba'@'localhost' 


六、查看 MySQL 用户权限

查看当前用户(自己)权限:
show grants;

查看其他 MySQL 用户权限:
show grants for dba@localhost;

七、撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:
grant all on *.* to dba@localhost;  revoke all on *.* from dba@localhost; 


要想使修改立即生效

flush privileges;


5.卸载

sudo apt-get autoremove --purge mysql-server-XX


最后的版本号跟自己情况确定

创建一个只读帐号密码123456
INSERT INTO `user` (`Host`, `User`, `Password`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Reload_priv`, `Shutdown_priv`, `Process_priv`, `File_priv`, `Grant_priv`, `References_priv`, `Index_priv`, `Alter_priv`, `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`, `Alter_routine_priv`, `Create_user_priv`, `Event_priv`, `Trigger_priv`, `ssl_type`, `ssl_cipher`, `x509_issuer`, `x509_subject`, `max_questions`, `max_updates`, `max_connections`, `max_user_connections`) VALUES ('%', 'user_rw', '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '','' ,'' ,'' , 0, 0, 0, 0);
UPDATE user SET password=PASSWORD('123456') WHERE user='root';



原创粉丝点击