linux mysql基本语法学习

来源:互联网 发布:华为畅享5手机壳淘宝 编辑:程序博客网 时间:2024/04/30 08:58

在windows中MySql以服务形式存在,在使用前应确保此服务已经启动,未启动可用net start mysql命令启动。而Linux中启动时可用“/etc/rc.d/init.d/mysqld start"命令,注意启动者应具有管理员权限。 刚安装好的MySql包含一个含空密码的root帐户和一个匿名帐户,这是很大的安全隐患,对于一些重要的应用我们应将安全性尽可能提高,在这里应把匿名帐户删除、 root帐户设置密码,可用如下命令进行: use mysql; delete from User where User=""; update User set Password=PASSWORD('newpassword') where User='root'; 如果要对用户所用的登录终端进行限制,可以更新User表中相应用户的Host字段,在进行了以上更改后应重新启动数据库服务,此时登录时可用如下类似命令: mysql -uroot -p; mysql -uroot -pnewpassword; mysql mydb -uroot -p; mysql mydb -uroot -pnewpassword; 上面命令参数是常用参数的一部分,详细情况可参考文档。此处的mydb是要登录的数据库的名称。 在进行开发和实际应用中,用户不应该只用root用户进行连接数据库,虽然使用root用户进行测试时很方便,但会给系统带来重大安全隐患,也不利于管理技术的提高。我们给一个应用中使用的用户赋予最恰当的数据库权限。如一个只进行数据插入的用户不应赋予其删除数据的权限。MySql的用户管理是通过User表来实现的,添加新用户常用的方法有两个,一是在User表插入相应的数据行,同时设置相应的权限;二是通过GRANT命令创建具有某种权限的用户。其中GRANT的常用用法如下: grant all on mydb.* to NewUserName@HostName identified by "password" ; grant usage on *.* to NewUserName@HostName identified by "password"; grant select,insert,update on mydb.* to NewUserName@HostName identified by "password"; grant update,delete on mydb.TestTable to NewUserName@HostName identified by "password"; 若要给此用户赋予他在相应对象上的权限的管理能力,可在GRANT后面添加WITH GRANT OPTION选项。而对于用插入User表添加的用户,Password字段应用PASSWORD 函数进行更新加密,以防不轨之人窃看密码。对于那些已经不用的用户应给予清除,权限过界的用户应及时回收权限,回收权限可以通过更新User表相应字段,也可以使用REVOKE操作。 下面给出本人从其它资料(www.cn-java.com)获得的对常用权限的解释: 全局管理权限:  FILE: 在MySQL服务器上读写文件。  PROCESS: 显示或杀死属于其它用户的服务线程。  RELOAD: 重载访问控制表,刷新日志等。  SHUTDOWN: 关闭MySQL服务。 数据库/数据表/数据列权限:  ALTER: 修改已存在的数据表(例如增加/删除列)和索引。  CREATE: 建立新的数据库或数据表。  DELETE: 删除表的记录。  DROP: 删除数据表或数据库。  INDEX: 建立或删除索引。  INSERT: 增加表的记录。  SELECT: 显示/搜索表的记录。  UPDATE: 修改表中已存在的记录。 特别的权限:  ALL: 允许做任何事(和root一样)。  USAGE: 只允许登录--其它什么也不允许做。Linux下启动、停止、重启mysql:

[root@test ~]# /etc/rc.d/init.d/mysqld stop停止 MySQL:                                               [确定][root@test ~]# /etc/rc.d/init.d/mysqld start启动 MySQL:                                               [确定][root@test ~]# /etc/rc.d/init.d/mysqld restart停止 MySQL:                                               [确定]启动 MySQL:                                               [确定]

[root@test ~]#

[liul@test ~]$ mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7 to server version: 5.0.22Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;+--------------------+| Database |+--------------------+| information_schema | | test | +--------------------+2 rows in set (0.00 sec)mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| liul_test | +----------------+1 row in set (0.00 sec)mysql> drop liul_test;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'liul_test' at line 1mysql> drop table liul_test;Query OK, 0 rows affected (0.00 sec)mysql> show tables;Empty set (0.00 sec)mysql> create table liul_test( -> id int(8), -> name varchar(10) -> );Query OK, 0 rows affected (0.00 sec)mysql> insert into liul_test values(1,'liul_hello');Query OK, 1 row affected (0.00 sec)mysql> insert into liul_test values(2,'liul_world');Query OK, 1 row affected (0.00 sec)mysql> describe liul_test;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int(8) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+2 rows in set (0.01 sec)mysql> update liul_test set name='helloworld' where id=2;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from liul_test;+------+------------+| id | name |+------+------------+| 1 | liul_hello | | 2 | helloworld | +------+------------+2 rows in set (0.00 sec)

	
				
		
原创粉丝点击