教会你完全搞定MySQL数据库 轻松八句话_MySQL
bitsCN.com
一、连接MYSQL 格式:
mysql -h主机地址
-u用户名
-p用户密码
1、例1:连接到本机上的MySQL:
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>。
2、例2:连接到远程主机上的MYSQL。假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:mysql -h110.110.110.110 -uroot -pabcd123。
3、退出MYSQL命令:exit (回车)。
二、修改密码
格式:mysqladmin -u用户名 -p旧密码 password 新密码
1、例1:给root加个密码ab12。首先在DOS下进入目录mysqlbin,然后键入以下命令:
mysqladmin -uroot -password ab12
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
2、例2:再将root的密码改为djg345:
mysqladmin -uroot -pab12 password djg345
三、增加新用户
(注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符)
格式:
grant select on 数据库.*
to 用户名@登录主机 identified by "密码"
例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MYSQL,然后键入以下命令:
grant select,insert,update,
delete on *.* to test1@"%" Identified by "abc";
但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台计算机上登录你的mysql数据库并对你的资料可以为所欲为了,解决办法见例2。
例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
如果你不想test2有密码,可以再打一个命令将密码消掉。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "";
注意:你必须首先登录到MYSQL中,以下操作都是在MYSQL的提示符下进行的,而且每个命令以分号结束。
四、操作技巧
1、如果你打命令时,回车后发现忘记加分号,你无须重打一遍命令,只要打个分号回车就可以了。也就是说你可以把一个完整的命令分成几行来打,完后用分号作结束标志就OK。
2、你可以使用光标上下键调出以前的命令。但以前我用过的一个MYSQL旧版本不支持。我现在用的是:mysql-3.23.27-beta-win。
五、显示命令
1、显示数据库列表:show databases;
刚开始时才两个数据库:mysql和test。mysql库很重要它里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。
2、显示库中的资料表:
use mysql;
//打开库,学过FOXBASE的一定不会陌生吧
show tables;
3、显示资料表的结构:describe 表名;
4、建库:create database 库名;
5、建表:
use 库名;
create table 表名 (字段设定列表);
6、删库和删表:
drop database 库名;
drop table 表名;
7、将表中记录清空:delete from 表名;
8、显示表中的记录:select * from 表名;
六、一个建库和建表以及插入数据的实例 drop database if exists school; //如果存在SCHOOL则删除
create database school;
//建立库SCHOOL
use school;
//打开库SCHOOL
create table teacher
//建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default 深圳,
year date
); //建表结束
//以下为插入字段
insert into teacher valuess
(,glchengang,深圳一中,1976-10-10);
insert into teacher valuess
(,jack,深圳一中,1975-12-23);
注:在建表中(1)将ID设为长度为3的数字字段:int(3)并让它每个记录自动加一:auto_increment并不能为空:not null而且让他成为主字段primary key(2)将NAME设为长度为10的字符字段(3)将ADDRESS设为长度50的字符字段,而且缺省值为深圳。varchar和char有什么区别呢,只有等以后的文章再说了。 (4)将YEAR设为日期字段。
如果你在mysql提示符键入上面的命令也可以,但不方便调试。你可以将以上命令原样写入一个文本文件中假设为school.sql,然后复制到c:下,并在DOS状态进入目录mysqlbin,然后键入以下命令:
mysql -uroot -p密码
如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你只要将//的注释去掉即可使用)。
七、将文本资料转到数据库中
1、文本资料应符合的格式:字段资料之间用tab键隔开,null值用n来代替.
例:
3 rose 深圳二中 1976-10-10
4 mike 深圳一中 1975-12-23
2、数据传入命令 load data local infile “文件名” into table 表名;
注意:你最好将文件复制到mysqlbin目录下,并且要先用use命令打表所在的库 。
八、备份数据库:
(命令在DOS的mysqlbin目录下执行)
mysqldump --opt school>school.bbb
注释:将数据库school备份到school.bbb文件,school.bbb是一个文本文件,文件名任取,打开看看你会有新发现。
=======================================================
MySQL服务器启动命令如下:net start mysql
MySQL服务器停止命令如下:net stop mysql
MySQL服务器的连接命令如下:
方式一:mysql -h 主机名 -u 用户名 -p 用户密码
方式二:mysql --host=主机名 --user=用户名 --password=用户密码
默认的主机是:localhost
=======================================================
一、数据备份捷径
因为这个方法没有得到官方正式文档的验证,我们暂称为试验吧。
目的:备份hostA主机中一个mysql数据库TestA,并恢复到到hostB机中
试验环境:
操作系统:WinNT4.0,Mysql3.22.34,phpMyAdmin 2.1.0
在hostA中安装mysql数据库并建立TestA数据库
hostB机安装mysql数据库,没有TestA数据库
方法步骤:
启动phpMyAdmin察看HostA和HostB中的数据库列表,在HostB中没有TestA数据库
找到HostA中mysql的安装目录,并找到数据库目录data
在我的试验环境中,这个目录是
C:mysqldata
找到对应数据库名称的子目录
C:mysqldataTestA
粘贴拷贝到HostB的Data目录下,是HostA同HostB Mysql数据目录下的文件相同
刷新HostB的phpMyAdmin察看一下数据库列表,我们看到TestA已经出现,并且作查询修改等操作都正常,备份恢复恢复成功
试验结论:Mysql的数据库可以通过文件形式保存,备份,恢复只要将相应文件目录恢复即可,无需使用其它工具备份。
二、正规的方法(官方建议):
导出要用到MySQL的mysqldump工具,基本用法是:
mysqldump [OPTIONS] database [tables]
如果你不给定任何表,整个数据库将被导出。
通过执行mysqldump --help,你能得到你mysqldump的版本支持的选项表。
注意,如果你运行mysqldump没有--quick或--opt选项,mysqldump将在导出结果前装载整个结果集到内存中,如果你正在导出一个大的数据库,这将可能是一个问题。
mysqldump支持下列选项:
--add-locks
在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(为了使得更快地插入到MySQL)。
--add-drop-table
在每个create语句之前增加一个drop table。
--allow-keywords
允许创建是关键词的列名字。这由在列名前面加表名的方法做到。
-c, --complete-insert
使用完整的insert语句(用列名字)。
-C, --compress
如果客户和服务器均支持压缩,压缩两者间所有的信息。
--delayed
用INSERT DELAYED命令插入行。
-e, --extended-insert
使用全新多行INSERT语法。(给出更紧缩并且更快的插入语句)
-#, --debug[=option_string]
跟踪程序的使用(为了调试)。
--help
显示一条帮助消息并且退出。
--fields-terminated-by=...
--fields-enclosed-by=...
--fields-optionally-enclosed-by=...
--fields-escaped-by=...
--fields-terminated-by=...
这些选择与-T选择一起使用,并且有相应的LOAD DATA INFILE子句相同的含义。
LOAD DATA INFILE语法。
-F, --flush-logs
在开始导出前,洗掉在MySQL服务器中的日志文件。
-f, --force,
即使我们在一个表导出期间得到一个SQL错误,继续。
-h, --host=..
从命名的主机上的MySQL服务器导出数据。缺省主机是localhost。
-l, --lock-tables.
为开始导出锁定所有表。
-t, --no-create-info
不写入表创建信息(CREATE TABLE语句)
-d, --no-data
不写入表的任何行信息。如果你只想得到一个表的结构的导出,这是很有用的!
--opt
同--quick --add-drop-table --add-locks --extended-insert --lock-tables。
应该给你为读入一个MySQL服务器的尽可能最快的导出。
-pyour_pass, --password[=your_pass]
与服务器连接时使用的口令。如果你不指定“=your_pass”部分,mysqldump需要来自终端的口令。
-P port_num, --port=port_num
与一台主机连接时使用的TCP/IP端口号。(这用于连接到localhost以外的主机,因为它使用 Unix套接字。)
-q, --quick
不缓冲查询,直接导出至stdout;使用mysql_use_result()做它。
-S /path/to/socket, --socket=/path/to/socket
与localhost连接时(它是缺省主机)使用的套接字文件。
-T, --tab=path-to-some-directory
对于每个给定的表,创建一个table_name.sql文件,它包含SQL CREATE 命令,和一个table_name.txt文件,它包含数据。 注意:这只有在mysqldump运行在mysqld守护进程运行的同一台机器上的时候才工作。.txt文件的格式根据--fields-xxx和--lines--xxx选项来定。
-u user_name, --user=user_name
与服务器连接时,MySQL使用的用户名。缺省值是你的Unix登录名。
-O var=option, --set-variable var=option
设置一个变量的值。可能的变量被列在下面。
-v, --verbose
冗长模式。打印出程序所做的更多的信息。
-V, --version
打印版本信息并且退出。
-w, --where='where-condition'
只导出被选择了的记录;注意引号是强制的!
"--where=user='jimf'" "-wuserid>1" "-wuserid
最常见的mysqldump使用可能制作整个数据库的一个备份:
mysqldump --opt database > backup-file.sql
但是它对用来自于一个数据库的信息充实另外一个MySQL数据库也是有用的:
mysqldump --opt database | mysql --host=remote-hobitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

Wireless networks have become an indispensable part of our lives with the rapid development of the Internet. In order to protect personal information and network security, it is very important to change your wifi password regularly, however. To help you better protect your home network security, this article will introduce you to a detailed tutorial on how to use your mobile phone to change your WiFi password. 1. Understand the importance of WiFi passwords. WiFi passwords are the first line of defense to protect personal information and network security. In the Internet age, understanding its importance can better understand why passwords need to be changed regularly. 2. Confirm that the phone is connected to wifi. First, make sure that the phone is connected to the wifi network whose password you want to change before changing the wifi password. 3. Open the phone’s settings menu and enter the phone’s settings menu.

According to news from this site on June 5, MSI participated in the 2024 Taipei International Computer Show and showcased a new flagship gaming computer called MEGVisionXAI. This game console is an extension of the existing Vision series and uses a very eye-catching surround glass design, with internal components clearly visible. The most attractive part is that the front of the host is equipped with an oversized touch screen. MSI staff said that it can synchronize MSI’s exclusive AI applications to further enhance various AI functions. The relevant pictures attached to this site are as follows: MSI has not yet explained more details. From the pictures shared, you can see that a local AI chatbot is running on the screen. Users can interact with it and ask it to complete AI tasks and locate locally stored documents. wait. Source of the above picture:

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I

According to news from this site on July 23, Lenovo’s YOGA Portal high-performance desktop computer, which has been exposed for a long time, is now confirmed to be officially released at ChinaJoy in Shanghai on July 27. It is claimed to be a mini host designed for professional AI creation. It is a performance master and an expert in AI creation of 3D digital people. The AI virtual background is based on the on-site pictures provided by our friend @yuP in Shanghai. The volume of this small host is only 3.7L. It is made of anodized aluminum and is equipped with Intel Core. i7-14700 processor, equipped with 32GBDDR5 memory and 1TB solid state drive. YOGA Portal is both a host and an all-in-one AI creation machine. The high-performance host is combined with an algorithm-optimized camera to form an integrated solution. Just stand in front of the camera i.e.

1. Download and install Xiaobai’s one-click system reinstallation tool on another computer, insert an empty USB disk to create a USB boot disk. For specific tutorials, please refer to: 2. Insert the USB boot disk into the computer that needs to change the password to restart, and press Start hotkey. Generally, the startup hotkey is one of F12, F8, F9, F10, and ESC. Then the startup interface appears, select the option of the USB disk and press Enter to enter. 3. Select [1] to start win10x64PE and press Enter to confirm. 4. Select the password modification tool on the desktop and double-click to open it. 5. Then a list of account names appears, select the account that needs to change the password and open it. 6. Click the Change Password command below, enter the new password twice, and then click OK to save the changes. 7. Finally, unplug the USB flash drive and restart the computer. Then it will be normal.

Wireless networks have become an integral part of people's lives in modern society. Or need to view saved passwords in WiFi list, sometimes we forget WiFi password, however. Mobile software came into being to solve this problem. To make it easy for you to master this magical tool, this article will introduce a software that can help you quickly unlock all WiFi passwords. 1. Powerful WiFi password unlocking tool - explore the wonderful world of WiFi password unlocking 2. Simple and easy-to-use interface design - convenient and fast operation methods 3. Supports multiple devices - meets the needs of different devices 4. Automatically Update - always keep the latest WiFi password 5. High-speed WiFi connection - easily cope with the simultaneous connection needs of multiple devices
