mysql 初级操作-查询数据库时间
2016-07-06 17:33
337 查看
然后在my.ini文件中的[mysqld]下面一行添加 skip_grant_tables(加上这句话)
1、最简单的:
CREATE TABLE t1(
id int not null,
name char(20)
);
2、带主键的:
a:
CREATE TABLE t1(
id int not null primary key,
name char(20)
);
b:复合主键
CREATE TABLE t1(
id int not null,
name char(20),
primary key (id,name)
);
3、带默认值的:
CREATE TABLE t1(
id int not null default 0 primary key,
name char(20) default '1'
);
drop table 表名字
创建用户:
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
这样就创建了一个名为:test 密码为:1234 的用户。
授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
show grants for'cactiuser'@'%';
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
MySql远程连接时的“access denied for user **@**”错误,搞的我很头大,后来查出来解决方法。记录一下,怕以后再忘记:
首先本地登陆MySQL,然后执行这两句代码:GRANT ALL PRIVILEGES ON *.* TO root@’%’ IDENTIFIED BY ’000000′;FLUSH PRIVILEGES;格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by “用户密码”;
参数说明: ALL PRIVILEGES表示赋给远程登录用户的权限,ALL PRIVILEGES表示所有的权限,你也可以单独或组合赋select,update,insert,delete权限;*.*:第一个*表示要赋权的数据库名,*当然表示全部数据库了,第二个*表示数据库下的表名,同理,*表示全部表,像我这样的懒人当然就直接用*.*了,反正都是自己开发用
4
root表示要赋权的用户;%表示远程登录的IP,如果要限制登录IP的话,这里就添你允许登录的IP,比如192.18.1.99等,%表示不限制IP(再次偷懒),000000是用户远程登录的密码。就这么简单。这句运行以后再运行FLUSH PRIVILEGES,搞定!
使用命令show global variables like 'port';查看端口号
查看MYSQL数据库中所有用户
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看数据库中具体某个用户的权限
show grants for'cactiuser'@'%';
查看user表结构 需要具体的项可结合表结构来查询
desc mysql.user;
int main()
{
MYSQL *pMysql = NULL;
MYSQL_RES *pMysqlRes = NULL;
MYSQL_FIELD *pMysqlField = NULL;
MYSQL_ROW pMysqlRow = NULL;
unsigned long client_flag = 0;
char sql[1024] = "select now()";
// char sql[1024] = "select * from system_config";
// char sql[1024] = "SELECT count( * ) FROM information_schema.tables WHERE TABLE_SCHEMA = 'tms'";
int iResult = 0;
if ((pMysql = mysql_init(NULL)) == NULL )
printf ("mysql init failed\n");
if ( (mysql_real_connect (pMysql, "127.0.0.1", "root", "123456", "tms", 3306,
NULL, client_flag)) == NULL )
printf("connect db wrong\n");
/*
if ((mysql_select_db(pMysql, "tms")) != 0)
{
mysql_close(pMysql);
pMysql = NULL;
printf ("select db failed");
}
*/
// mysql_free_result(pMysqlRes);
iResult = mysql_real_query(pMysql,sql, strlen(sql)); //查询语句
if ( 0 == iResult )
{
int query_count = 0;
pMysqlRes = mysql_store_result(pMysql); //返回查询结果
query_count = mysql_field_count(pMysql); //返回查询结果数量
pMysqlRow = mysql_fetch_row(pMysqlRes); //返回查询结果集-以数组形式
printf( "query mysql is ok,query_coun=%d\n", query_count);
printf( "scoer=%s\n", pMysqlRow[0]);
}
else
printf( "query mysql failed\n");
return E_OK;
}
1、最简单的:
CREATE TABLE t1(
id int not null,
name char(20)
);
2、带主键的:
a:
CREATE TABLE t1(
id int not null primary key,
name char(20)
);
b:复合主键
CREATE TABLE t1(
id int not null,
name char(20),
primary key (id,name)
);
3、带默认值的:
CREATE TABLE t1(
id int not null default 0 primary key,
name char(20) default '1'
);
drop table 表名字
创建用户:
mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
这样就创建了一个名为:test 密码为:1234 的用户。
授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
show grants for'cactiuser'@'%';
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
MySql远程连接时的“access denied for user **@**”错误,搞的我很头大,后来查出来解决方法。记录一下,怕以后再忘记:
首先本地登陆MySQL,然后执行这两句代码:GRANT ALL PRIVILEGES ON *.* TO root@’%’ IDENTIFIED BY ’000000′;FLUSH PRIVILEGES;格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by “用户密码”;
参数说明: ALL PRIVILEGES表示赋给远程登录用户的权限,ALL PRIVILEGES表示所有的权限,你也可以单独或组合赋select,update,insert,delete权限;*.*:第一个*表示要赋权的数据库名,*当然表示全部数据库了,第二个*表示数据库下的表名,同理,*表示全部表,像我这样的懒人当然就直接用*.*了,反正都是自己开发用
4
root表示要赋权的用户;%表示远程登录的IP,如果要限制登录IP的话,这里就添你允许登录的IP,比如192.18.1.99等,%表示不限制IP(再次偷懒),000000是用户远程登录的密码。就这么简单。这句运行以后再运行FLUSH PRIVILEGES,搞定!
使用命令show global variables like 'port';查看端口号
查看MYSQL数据库中所有用户
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看数据库中具体某个用户的权限
show grants for'cactiuser'@'%';
查看user表结构 需要具体的项可结合表结构来查询
desc mysql.user;
int main()
{
MYSQL *pMysql = NULL;
MYSQL_RES *pMysqlRes = NULL;
MYSQL_FIELD *pMysqlField = NULL;
MYSQL_ROW pMysqlRow = NULL;
unsigned long client_flag = 0;
char sql[1024] = "select now()";
// char sql[1024] = "select * from system_config";
// char sql[1024] = "SELECT count( * ) FROM information_schema.tables WHERE TABLE_SCHEMA = 'tms'";
int iResult = 0;
if ((pMysql = mysql_init(NULL)) == NULL )
printf ("mysql init failed\n");
if ( (mysql_real_connect (pMysql, "127.0.0.1", "root", "123456", "tms", 3306,
NULL, client_flag)) == NULL )
printf("connect db wrong\n");
/*
if ((mysql_select_db(pMysql, "tms")) != 0)
{
mysql_close(pMysql);
pMysql = NULL;
printf ("select db failed");
}
*/
// mysql_free_result(pMysqlRes);
iResult = mysql_real_query(pMysql,sql, strlen(sql)); //查询语句
if ( 0 == iResult )
{
int query_count = 0;
pMysqlRes = mysql_store_result(pMysql); //返回查询结果
query_count = mysql_field_count(pMysql); //返回查询结果数量
pMysqlRow = mysql_fetch_row(pMysqlRes); //返回查询结果集-以数组形式
printf( "query mysql is ok,query_coun=%d\n", query_count);
printf( "scoer=%s\n", pMysqlRow[0]);
}
else
printf( "query mysql failed\n");
return E_OK;
}
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- Android之获取手机上的图片和视频缩略图thumbnails
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 数据库链接字符串查询网站
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法