search
HomeDatabaseMysql Tutorialmysql常用操作添加新用户/分配权限/修改表/建索引等_MySQL

bitsCN.com

mysql常用操作添加新用户/分配权限/修改表/建索引等

 

bin>mysql -u root 

mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’; 

 

权限1,权限2,…权限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限。 

当权限1,权限2,…权限n被all privileges或者all代替,表示赋予用户全部权限。 

当数据库名称.表名称被*.*代替,表示赋予用户操作服务器上所有数据库所有表的权限。

用户地址可以是localhost,也可以是ip地址、机器名字、域名。也可以用’%'表示从任何地址连接。 

‘连接口令’不能为空,否则创建失败。 

 

例如: 

mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′; 

给来自10.163.225.87的用户joe分配可对数据库vtdc的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123。 

 

mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′; 

给来自10.163.225.87的用户joe分配可对数据库vtdc所有表进行所有操作的权限,并设定口令为123。 

 

mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′; 

给来自10.163.225.87的用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。 

 

mysql>grant all privileges on *.* to joe@localhost identified by ‘123′; 

给本机用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。

 

 

创建含有外键的表:

 

create table question(id int auto_increment primary key not null,content varchar(300) not null, intime datetime not null,u_id int not null,foreign key(u_id) references user(id) on delete cascade on update cascade);

 

 

 

 

 

备份数据表

shell> mysqldump [OPTIONS] database [tables]  

 

例子:

 

/usr/local/mysql/bin/mysqldump -u 用户名 -p 数据库名称 > ./fedtrainning_db.sql

如果你不给定任何表,整个数据库将被导出。  

 

 

修改表的属性 =======增、删、改======================

ALTER TABLE notify CHANGE content content varchar(500) not null

 

//主键

 

   alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);

//增加一个新列

 

   alter table t2 add d timestamp;

alter table infos add ex tinyint not null default '0';

//删除列

 

   alter table t2 drop column c;

//重命名列

 

   alter table t1 change a b integer;

 

//改变列的类型

 

   alter table t1 change b b bigint not null;

alter table infos change list list tinyint not null default '0';

//重命名表

 

   alter table t1 rename t2;

加索引

 

   mysql> alter table tablename change depno depno int(5) not null;

mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);

mysql> alter table tablename add index emp_name (name);

加主关键字的索引

 

mysql> alter table tablename add primary key(id);

加唯一限制条件的索引

 

  mysql> alter table tablename add unique emp_name2(cardnumber);

删除某个索引

 

   mysql>alter table tablename drop index emp_name;

修改表:

 

增加字段:

 

   mysql> ALTER TABLE table_name ADD field_name field_type;

修改原字段名称及类型:

 

   mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

删除字段:

 

   mysql> ALTER TABLE table_name DROP field_name; 

======================= 编码相关 ==========================

 

1修改整个数据库服务器

 

在my.cf文件的[mysqld]段设置:

default-character-set=utf8

 

2单独设置某个数据库:

 

alter database testdb character set utf8;

 

3 查看mysql支持的编码:

show character set;

 

4查看数据库的编码格式:

 

show create database testdb;

 

5 查看数据库的各项编码设置:

 

mysql> SHOW VARIABLES LIKE 'character_set_%'; 

 

SET NAMES 'utf8'; 

 

它相当于下面的三句指令: 

 

SET character_set_client = utf8; 

SET character_set_results = utf8; 

SET character_set_connection = utf8; 

 

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools