二 1 登录MySQL
[root@test1 local]# cd mysql/bin
[root@test1 bin]#./mysql -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.0.16-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
出现了“mysql>”提示符,恭喜你,安装成功!
退出mysql
mysql>quit
2 注意
如果出现 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) ----没有启动tomcat
3 修改登录密码
MySQL默认没有密码,安装完毕增加密码的重要性是不言而喻的。
1)命令
usr/bin/mysqladmin -u root password 'new-password'
格式:mysqladmin -u用户名 -p旧密码 password 新密码
2)例子
例1:给root加个密码123456。
键入以下命令 :
[root@test1 local]# /usr/local/mysql/bin/mysqladmin -u root password 123456
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
3)测试是否修改成功
(3-1)不用密码登录
[root@test1 local]# mysql
ERROR 1045: Access denied for user: 'root@localhost'
(Using password: NO)
显示错误,说明密码已经修改。
(3-2)用修改后的密码登录
[root@test1 bin]# ./mysql -u root -p
Enter password: (输入修改后的密码123456)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.0.16-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
成功!
这是通过mysqladmin命令修改口令,也可通过修改库来更改口令。
6、建表:
use 库名;
create table 表名 (字段设定列表);
例如:在刚创建的aaa库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(性别),csny(出身年月)四个字段
use aaa;
mysql> create table name (id int(3) auto_increment not null primary key, xm char(8),xb char(2),csny date);
可以用describe命令察看刚建立的表结构。
mysql> describe name;
+-------+---------+------+-----+---------+----------------+
| Field | Type| Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id| int(3) | | PRI | NULL| auto_increment |
| xm| char(8) | YES | | NULL||
| xb| char(2) | YES | | NULL||
| csny | date| YES | | NULL||
+-------+---------+------+-----+---------+----------------+
7、增加记录
例如:增加几条相关纪录。
mysql> insert into name values('','张三','男','1971-10-01');
mysql> insert into name values('','白云','女','1972-05-20');
可用select命令来验证结果。
mysql> select * from name;
+----+------+------+------------+
| id | xm | xb | csny |
+----+------+------+------------+
| 1 | 张三 | 男 | 1971-10-01 |
| 2 | 白云 | 女 | 1972-05-20 |
+----+------+------+------------+
8、修改纪录
例如:将张三的出生年月改为1971-01-10
mysql> update name set csny='1971-01-10' where xm='张三';
9、删除纪录
例如:删除张三的纪录。
mysql> delete from name where xm='张三';
10、删库和删表
drop database 库名;
drop table 表名;
九、增加MySQL用户
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户user_1密码为123,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
mysql> grant select,insert,update,delete on *.* to
[email=user_1@]user_1@"%[/email]
" Identified by "123";
例1增加的用户是十分危险的,如果知道了user_1的密码,那么他就可以在网上的任何一台电脑上登录你的MySQL数据库并对你的数据为所欲为了,解决办法见例2。
例2、增加一个用户user_2密码为123,让此用户只可以在localhost上登录,并可以对数据库aaa进行查询、插入、修改、删除的操作(localhost指本地主机,即MySQL数据库所在的那台主机),这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过 MYSQL主机来操作aaa库。
mysql>grant select,insert,update,delete on aaa.* to
[email=user_2@localhost]user_2@localhost[/email]
identified by "123";
用新增的用户如果登录不了MySQL,在登录时用如下命令:
mysql -u user_1 -p -h 192.168.113.50 (-h后跟的是要登录主机的ip地址)