1
我将通过C程序向MySQL添加用户。我将在MySQL中完成最后一年的项目。如果我问错,不要误解我的意思。我对这个概念有一个疑问,那就是我们可以添加一个具有某种特权的新用户作为根。之后,我们是否需要注销根目录或不注册。如果我这样做,如何注销root。c程序将用户添加到mysql
//header files inclusion
int main(void)
{
//declared MYSQL,MYSQL_REs,MYSQL_ROW variables.
// here declared username,pwd,localhost,databasename
// here declared variable to query
con = mysql_init(NULL);
/* connection to database */
if(!mysql_real_connect(con,server,username,password,database,0,NULL,0)) {
{
printf("error in connection\n");
exit(1);
}
printf("\n Enter the username to create a user in DB\n");
scanf("%s",name);
printf("\n Enter password \n");
scanf("%s",pwd);
sprintf(sql_addUser,"create user '%s'@'localhost' identified by '%s';",name,pwd);
mysql_query(con,sql_addUser);
sprintf(sql_grantAcc,"grant INSERT on '%s'.* to '%s'@'localhost';",databse,name);
mysql_query(con,sql_grantAcc);
因此,我们在root模式下创建了一个用户。然后我需要使用新创建的用户输入MySQL,以便我需要注销或关闭连接。请给我一个如何改变用户模式的解决方案。
我假设要更改用户模式,我们需要关闭建立的连接是否正确?
mysql_free_result(result);
mysql_close(con);
con = mysql_init(NULL);
if(!mysql_real_connect(con,server,name,pwd,database,0,NULL,0)) {
{
printf("error in connection\n");
exit(1);
}
嗨@Qsp谢谢你的回应。我很努力,但我给了解决方案来做进一步的工作。我认为mysql_change_user最适合我bcoz我不改变数据库的未来。 – Srini