SHOW GRANTS FOR USERNAME;
GRANT SELECT, INSERT, UPDATE, DELETE, SHOW VIEW ON `pcccti`.* TO 'pccctiopr'@'%'
MySQL 菜鸟教程
mysql
MySQL
MySQL基础操作
MySQL修改字段名称
MySQL中增删改字段名类型,注释调整字段顺序
header 1 | header 2 |
---|---|
databases | |
tables | |
columns | |
row | |
冗余 | |
key | |
外键 | |
复何健 | |
index | |
参照完整性 |
Features | Command |
---|---|
rpm -qa pipe grep mysql | whether install mysql |
rpm -e mysql | delete MySQL |
rpm -e --nodeps mysql | Powerful delete(MySQL) mode |
systemctl status mysqld | check mysql status |
mysqladmin --version | whether install mysql |
ps -ef pipe grep mysql | Check if the MySQL Server is started |
>cd /usr/bin >./mysqld_safe & | start MySQL |
cd /usr/bin >./mysqladmin -u root -p shutdown | shutdown MySQL |
mysql | connect MySQL Server by MySQL Client |
SHOW DATABASES; | |
mysql -u root -p & Enter password:****** | Setting init password |
mysql -h host -u user_name -p | login to the Database by MySQL Client |
MySQL Command | Features |
---|---|
mysql> exit | Bye |
CREATE DATABASE DATABASE_NAME; | |
USE DATABASE_NAME; | SELECT DATABASE |
SHOW DATABASES; | |
SHOW TABLES; | |
SHOW COLUMNS FROM TABLE_NAME; | 显示数据表的属性、属性类型、主键等信息 |
SHOW INDEX FROM TABLE_NAME; | |
SHOW TABLE STATUS LIKE [FROM db_name] [LIKE ‘pattern’] \G: | |
SHOW TABLE STATUS from RUNOOB LIKE ‘runoob%’\G; | |
DROP DATABASE DATABASE_NAME; | |
SELECT * FROM TABLE_NAME; | |
DELETE FROM table_name [WHERE Clause] | DELETE records |
只需要在 mysql 数据库中的 user 表添加新用户即可
mysql> INSERT INTO user
(host, user, password, select_priv, insert_priv, update_priv)
VALUES ('localhost', 'janus', PASSWORD('janus'), 'Y', 'Y', 'Y');
mysql> FLUSH PRIVILEGES;
mysql> SELECT host, user, password FROM user WHERE user = 'guest';
通过SQL的GRANT命令添加用户
root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON DatabasesName.*
-> TO 'UserName'@'localhost'
-> IDENTIFIED BY 'password';
CREATE TABLE table_name (column_name column_type);
CREATE TABLE IF NOT EXISTS `table_name`(
`client_id` INT UNSIGNED AUTO_INCREMENT,
`client_name` VARCHAR(100) NOT NULL,
`client_addr` VARCHAR(40) NOT NULL,
`user_agent` DATE,
PRIMARY KEY ( `client_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO TABLE_NAME (field1, field2, ... fieldN) VALUES (value1, value2, ...valueN );
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
MySQL JOIN
MYSQL * STDCALL mysql_real_connect(
MYSQL *mysql, const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag);
第一个參数mysql是C语言api中一个很重要的变量。里面内存很丰富。有port,dbname,charset等连接基本參数。它也包括了一个叫 st_mysql_methods的结构体变量,该变量里面保存着许多函数指针。这些函数指针将会在数据库连接成功以后的各种数据操作中被调用。
int STDCALL mysql_query(MYSQL *mysql, const char *q);
第一个參数上面已经介绍过。第二个參数为要运行的sql语句,主要就是运行SQL语句的增、删、改、查等功能。
const char *mysql_error(MYSQL *mysql)
unsigned int mysql_errno(MYSQL *mysql);
返回上次UPDATE更改的行数,上次DELETE删除的行数,或上次INSERT语句插入的行数。对于UPDATE、DELETE或INSERT语句,可在mysql_query()后立刻调用。对于SELECT语句,mysql_affected_rows()的工作方式与mysql_num_rows()类似。
my_ulonglong mysql_affected_rows(MYSQL *mysql)
为当前连接返回默认的字符集。
const char *mysql_character_set_name(MYSQL *mysql);
该函数用于为当前连接设置默认的字符集。字符串csname指定了1个有效的字符集名称。连接校对成为字符集的默认校对。该函数的工作方式与SET NAMES语句类似,但它还能设置mysql->charset的值,从而影响了由mysql_real_escape_string()设置的字符集。
return: 0表示成功,非0值表示出现错误。
int mysql_set_character_set(MYSQL *mysql, char *csname);
关闭前面打开的连接。如果句柄是由mysql_init()或mysql_connect()自动分配的,mysql_close()还将解除分配由mysql指向的连接句柄。
void mysql_close(MYSQL *mysql);
使由db指定的数据库成为由mysql指定的连接上的默认数据库(当前数据库)。在后续查询中,该数据库将是未包含明确数据库区分符的表引用的默认数据库。
return: 0表示成功,非0值表示出现错误。
int mysql_select_db(MYSQL *mysql, const char *db)
一次提取全部数据
MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql);
my_ulonglong mysql_num_rows(MYSQL_RES *result);
MYSQL_ROW mysql_fetch_row(MYSQL_RES *resutl);
void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);
MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);
void mysql_free_result(MYSQL_RES *result);
一次提取一行数据:mysql_use_result();
unsigned int mysql_field_count(MYSQL *connection);
MYSQL_FIELD *mysql_fetch_field(MYSQL *result);
MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL *result, MYSQL_FIELD_OFFSET offset);
MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL *result, MYSQL_FIELD_OFFSET offset);
MYSQL_FIELD定义在sql.h中,是指向字段结构数据的指针,有关于列的信息
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqlclient15-dev
#include
#include
#include
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char server[] = "localhost"; //47.75.158.180
char user[] = "root";
char password[] = "admin";// password is here
char database[] = "mysql";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s \n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
printf("finish! \n");
return 0;
}
Tips: The directory of libs is diferent when the moethod of installation is diferent.
Directory: /usr/local/include/
Directory: /usr/lib or
Directory: /usr/lib/x86_64-linux-gnu/
prefix=/usr
includedir=${prefix}/include/mysql
libdir=${prefix}/lib/x86-linux-gun
Name: mysqlclient
Description: MySQL client library
Version: 20.3.11
cflags: -I${includedir}
Libs: -L${libdir} -lmysqlclient
libs.private -lpthread -lz -lm -lrt -ldl
Requires.private:
gcc -o conn mysql_connect.c -L/usr/lib/x86_64-linux-gnu/ -lmysqlclient -lpthread -lz -lm -lrt -ldl
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/var/lib
[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
fatal error: mysql.h: No such file or directory
linuxC无法访问mysql.h:fatal error: mysql.h: No such file or directory compilation terminated.
fatal error: mysql.h: No such file or directory compilation terminated
在Ubuntu体系中,已经安装了mysql,即应用sudo apt-get install mysql-server mysql-client
但是用C编译mysql数据库时,报错
fatal error: mysql.h: No such file or directory
出现这个错误是因为体系没有安装mysql开发库.
执行下面指令安装
sudo apt-get install libmysql+±dev
编译时须要加连接-lmysqlclient.
编译源法度的时辰,如下号令:
gcc -I/usr/include/mysql *.c -L/usr/lib/mysql -lmysqlclient -o *
fatal error: Host ‘XXX’ is not allowed to connect to this MySQL server
Blog [Remote Connect Error]
https://www.cnblogs.com/jesu/p/5650699.html