Home Database Mysql Tutorial Linux下MySQL的一些基本使用方法

Linux下MySQL的一些基本使用方法

Jun 07, 2016 pm 04:53 PM
number

1]Linux下如何创建mysqld数据库的管理用户?数据库安装好后,我们应该为mysql数据库创建一个管理帐号。要把root用户设置为管理员

1]Linux下如何创建mysqld数据库的管理用户?

数据库安装好后,我们应该为mysql数据库创建一个管理帐号。要把root用户设置为管理员,我们应该运行下面的命令;

[root@linuxsir01 root]# /opt/mysql/bin/mysqladmin -u root password 123456
[root@linuxsir01 root]#

通过上面的命令,我们可以知道,mysql数据库的管理员是root,密码是123456。

2]如何进入mysql数据库?以mysql数据库管理员root,密码为123456为例;

[root@linuxsir01 root]#/opt/mysql/bin/mysql -uroot -p123456

输出上面的命令后,出现的是如下的提示;

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 3.23.58

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

注意:操作这些命令的时候,应该把mysqld服务器打开。这些新手兄弟早就知道了吧:)


3]如何在数据库中操作命令呢,我想这是mysql手册都有的,我主要说几个要注意的地方。其实我也会不了几个命令。如果自己想学的弟兄,也不是什么难事;在windows中操作过mysql的,其实在这里也是一样的,mysql是跨平台的数据库,,用法都是相同的。

在mysql数据库中,每操作一个命令,都是;号结尾的,可能有的新手弟兄,忘记输入了;号结尾,结果退不出来。:):)

1]查看mysql中都有哪些数据库?

代码:

mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)

mysql>

在mysql安装好,设置好管理员后,第一次进入系统,我们用show databases;命令查看数据库的列表,发现有两个数据库,mysql和test,这是系统自建的,是让大家练习用的。

4]如何创建和删除一个数据库?

比如我要创建一个名为linux的数据库,应该运行如下命令

mysql> create database [数据库名];

所以我们应该运行如下的命令,来创建名为linux的数据库

mysql> create database linux;
Query OK, 1 row affected (0.00 sec)

是不是建好了呢??肯定是建好了,因为都有OK了:)

查看是不是有linux这个数据库了呢?

代码:

mysql> show databases;
+----------+
| Database |
+----------+
| linux |
| mysql |
| test |
+----------+
3 rows in set (0.00 sec)

mysql>

那我们如何删除一个数据库呢??
mysql> drop database [数据库名];

比如我们要把刚才创建的linux数据库删除,应该用下面的命令;
mysql> drop database linux;
Query OK, 0 rows affected (0.00 sec)

是不是已经删除了呢??

代码:

mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)

mysql>


5]如何操作一个数据库呢,这个问题就比较多了,建议还是看一下mysql的手册吧。里面的东西太多了。如果操作一个数据库,首先是要指定一个数据库为当前数据库,应该用use命令

mysql>use [数据库];

比如我想指定linux这个数据库为当前数据库,应该是

mysql> use linux;
Database changed
mysql>



6]如何备份数据库??

比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump

命令格式如下:

[root@linuxsir01 root]# /opt/mysql/bin/mysqldump -uroot -p linux > /root/linux.sql
Enter password:在这里输入数据库的密码

通过上面的命令,我们要明白两件事,首先备份数据库是要以数据库管理员的身份备份;其次:备份目的地是/root,备份的文件名是linux.sql。其实备份的位置和文件名,根据自己的情况来定。文件名可以自己来取,路径也可以自己来安排;

比如我想把linux的数据库备份到/home/beinan,数据库的文件名为linuxsir031130.sql,所以应该输入如下的命令。
[root@linuxsir01 root]#/opt/mysql/bin/mysqldump -uroot -p linux > /home/beinan/linuxsir031130.sql
Enter password:在这里输入数据库管理员root的数据库密码

这样我们到/home/beinan目录下就能发现mysql中名为linux的数据库的备份文件linuxsir031130.sql

综上所述,我们学习时要学会变通。:):)

5]如何把把备份的数据库导入到数据库中?

首先我们还是要操作上面几个过程,比如添加数据库管理员(如果您没有添加过mysql数据库管理员的话),创建数据库等。

比如我们要把在/home/beinan这个目录中的linuxsir031130.sql这个备份,导入名为linux的数据库中,应该如下操作;

[root@linuxsir01 root]# /opt/mysql/bin/mysql -uroot -p linux Enter password:在这里输入密码

如果机器好,数据库比较小,几分钟就好了。

6]其它一些比较常用的mysql指令;

查看状态
mysql> show status;

查看进程

代码:

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 16 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

mysql>

查看表,应该先指定一个数据库为当前数据库;比如是名为linux的数据库;

mysql>use linux;
mysql> show tables;
Empty set (0.00 sec)

mysql>


7]对mysql数据库常用命令的一点补充;


几个常用的mysql相关的管理命令

mysql 命令:基本文本的,显示和使用的mysql数据库。前面已经简单的提过用法;比如登录等。

mysqladmin 命令,用来创建和维护mysql数据库的命令,前面已经简单的提过;

isamchk 是用来修复、检查和优化.ism后缀的数据库文件;

mysqldump 是用于备份数据库,前面已经简单的说明过;


myisamchk 用来修复.myi后缀的数据库文件;

比如我们要检查名为linux的数据库.myi数据库表是否存在问题,应该用下面的命令;

要把mysqld服务器停下来
[root@linuxsir01 root]# /opt/mysql/share/mysql.server stop

然后执行
[root@linuxsir01 root]# /opt/mysql/bin/myisamchk /opt/mysql/var/linux/*.MYI

上面的命令的意思就是检查所有的.myi文件,数据库的目录在/opt/mysql/var/linux/目录中

如果有问题,应该用-r参数来修复
[root@linuxsir01 root]# /opt/mysql/bin/myisamchk -r /opt/mysql/var/linux/*.MYI

6]mysqlshow 命令:显示用户选择的数据库和表
[root@linuxsir01 root]# /opt/mysql/bin/mysqlshow -uroot -p [数据库名]

比如我要查看名为linux的数据库;应该是:

[root@linuxsir01 root]# /opt/mysql/bin/mysqlshow -uroot -p linux

linux

Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Mar 06, 2025 pm 04:36 PM

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

C program to find the largest prime factor of a number C program to find the largest prime factor of a number Aug 27, 2023 am 10:09 AM

PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Feb 27, 2025 pm 06:33 PM

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

Top 10 trading platforms for digital currency apps, regular currency speculation platform app recommendations Top 10 trading platforms for digital currency apps, regular currency speculation platform app recommendations Mar 07, 2025 pm 06:51 PM

This article recommends ten digital currency trading apps: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Poloniex. When choosing a platform, you need to consider factors such as security, liquidity, transaction fees, currency selection, user interface, customer service support and regulatory compliance, and carefully evaluate risks and never blindly follow the trend.

Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Mar 17, 2025 pm 05:57 PM

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

The world's top ten virtual currency trading platform app genuine download and installation tutorial The world's top ten virtual currency trading platform app genuine download and installation tutorial Mar 12, 2025 pm 05:33 PM

This article provides Android and Apple mobile APP download methods for mainstream digital currency trading platforms such as Binance, OKX, Gate.io, Huobi Global, Coinbase, KuCoin, Kraken and Bitfinex. Whether it is an Android user or an Apple user, you can easily find the official APP download link for the corresponding platform and complete the installation according to the steps. The article provides detailed guidance on searching and downloading on their respective official websites or app stores, and provides instructions on the special steps for installing APK files on Android, so that users can download and use them quickly and easily.

What are the reliable digital currency platforms? Top 10 formal digital currency trading platforms 2025 What are the reliable digital currency platforms? Top 10 formal digital currency trading platforms 2025 Mar 17, 2025 pm 05:45 PM

Reliable digital currency platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini. These exchanges have their own characteristics. Users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) Apr 21, 2025 pm 09:33 PM

The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

See all articles