本文目录一览:
linux怎么启动mysql
linux启动mysql的方法:
- 使用linux命令service 启动:
service mysqld start
- 使用 mysqld 脚本启动:
/etc/inint.d/mysqld start
- 使用 safe_mysqld 启动:
safe_mysqld
linux停止的方法: - 使用 service 启动:
service mysqld stop
- 使用 mysqld 脚本启动:
/etc/inint.d/mysqld stop
mysqladmin shutdown
Linux数据库:mysql下如何执行sql脚本_第2页
同样,可以手动执行sql文件,具体步骤如下:
- 使用root帐户登录到MySQL服务器;
- 执行
source
命令:
mysql
source
c:/test.sql
注意:文件路径中建议使用“/”,如果使用“\”,要首先进行转义即“\”,否则可能会出现错误。
另外还有一个load
命令可以批量插入数据,但是这个对文件中数据格式的要求比较严格,否则容易出现错误,很少使用。
mysql
查看所有用户的语句
输入指令select user();
例:(项目来源:尚学堂)struts_training_itemmgr.sql
内容如下:
Sql代码
DROP TABLE t_items;
DROP TABLE t_data_dict;
CREATE TABLE t_items
(
item_no varchar(20) not null key,
item_name varchar(20) not null,
spec varchar(20),
pattern varchar(10),
category varchar(20),
unit char(20)
);
CREATE TABLE t_data_dict
(
id varchar(5) not null key,
category varchar(20),
name varchar(30)
);
# t_data_dict的初始化数据
INSERT INTO t_data_dict(id,category,name) values('B01','item_category','精通Spring2.X Java Web开发');
INSERT INTO t_data_dict(id,category,name) values('B02','item_category','Java语言与面向对象程序设计');
INSERT INTO t_data_dict(id,category,name) values('B03','item_category','2B铅笔');
INSERT INTO t_data_dict(id,category,name) values('B04','item_category','HOTROCK notebook');
INSERT INTO t_data_dict(id,category,name) values('C01','item_unit','本');
INSERT INTO t_data_dict(id,category,name) values('C02','item_unit','支');
INSERT INTO t_data_dict(id,category,name) values('C03','item_unit','箱');
Linux下如何运行sql脚本
Linux运行sql脚本的具体操作步骤如下:
- 使用shell工具登陆到安装postgresql的服务器,切换到postgres用户,postgresql默认的操作用户,命令是:
su - postgres
,查看当前路径是/var/lib/psql
,创建一个test.sql
脚本文件,命令是:vim test.sql
。 - sql脚本内容是:
create table test (id int not null primary key,name text);
insert into test valus(1, 't1');
- 执行
test.sql
脚本,命令是:psql -f test.sql
这里是因为postgresql安装本机上,在第一步中我们切换到了postgres用户,因此这里默认就是postgres用户来操作,不用带上用户名和密码。执行结果如下,可以看到有两个提示:
create table
insert 0 1
执行完成后,我们登入数据库,命令是:psql
4. 进入psql交互式命令行后,我们执行两个查看命令:\d
可以看到表test确实已经创建成功,然后执行命令:\d test
可以看到表中字段是id和name,和我们创建语句中内容一样,说明第一条语句执行成功。
5. 查看表中数据,命令是:select * from test;
显示出来的值是1,t1,说明第二条执行语句也执行成功,说明test.sql脚本执行成功。
6. 默认是postgres用户,本机操作是,不需要用户和密码,现在我们来试试操作远程linux服务器上的postgresql,也就是说执行本地的脚本文件,在远程服务器上创建表。如下面图中所示,命令是:psql -U test1 -h 192.168.1.194 -f test.sql
,输入对应用户的密码。
7. 登陆到这个远程服务器上,命令是:psql -U test -h 192.168.194
执行查看命令:\d
,\d test
最后查询数据库:select * from test;
结果和上面都一致。