shell脚本实现mysql数据库操作

Mysql数据库在ubuntu中的安装配置详见“ubuntu下安装mysql源代码版本(自己整理).doc”

在安装成功后,将/usr/local/mysql的权限赋予root。并设置root登录mysql的密码: sudo /usr/local/mysql/bin/mysqladmin –u root –p password ‘root’

然后要求输入旧密码,由于root的旧密码是空,所以直接回车即可。此时已经成功设置root登录mysql的密码为‘root’。(前文也有介绍,这里重复解释一下)

启动mysql,可以基于命令行实现,也可以把命令行写在脚本中,无所谓。

/usr/local/mysql/support-files/mysql.server start 其中,/usr/local/mysql是mysql的安装路径。 (1)创建数据库和数据表,编写脚本create.sh

/usr/local/mysql/bin/mysql -u root -p -e \创建数据表student:

/usr/local/mysql/bin/mysql -u root -p <

create table student(id int(5),name char(20),age int(5)); EOF

(2)插入记录,编写脚本insert.sh:

/usr/local/mysql/bin/mysql -u root -p <

use feng;

insert into student values(1,'Mary',25); insert into student values(2,'Tom',22); insert into student values(3,'John',20); EOF

(3)查询记录,编写脚本select.sh:

/usr/local/mysql/bin/mysql -u root -p <

use feng;

select * from student; #查询全部记录

select * from student where name='John'; 查询姓名是John的记录 EOF

(4)更新记录,编写脚本update.sh:

/usr/local/mysql/bin/mysql -u root -p <

update student set name='fjz' where id=1; EOF

(5)删除记录,编写脚本delete.sh:

/usr/local/mysql/bin/mysql -u root -p <

delete from student where id=1; EOF

另外还可以做数据库的其他操作,这里不再赘述。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4