Home Database Mysql Tutorial Some basic usage of mysql under linux

Some basic usage of mysql under linux

Dec 16, 2016 am 11:23 AM

1] How to create an administrative user for the MySQLd database?

After the database is installed, we should create an administrative account for the mysql database. To set the root user as administrator, we should run the following command;

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

via the above command, we can know that the administrator of the mysql database is root and the password is 123456.

2] How to enter the mysql database? Take the mysql database administrator root with the password 123456 as an example;

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

After outputting the above command, the following prompt appears;

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>

Note: When operating these commands, the mysqld server should be opened. These novice brothers have known it for a long time:)


3] How to operate commands in the database, I think this is all in the mysql manual. I will mainly talk about a few things to pay attention to. In fact, I can't understand a few commands. If you want to learn it yourself, it is not difficult; if you have operated mysql in windows, it is actually the same here. Mysql is a cross-platform database, and its usage is the same.

In the mysql database, every command ends with a ; sign. Some novice brothers may have forgotten to enter a ; sign, and the result cannot be exited. :):)

1] Check what databases are available in mysql?

Code:

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

mysql>

After mysql is installed and the administrator is set up, when entering the system for the first time, we use show databases; Use the command to view the list of databases and find that there are two databases, mysql and test. These are self-built by the system and are for everyone to practice.

4] How to create and delete a database?

For example, if I want to create a database named linux, I should run the following command

mysql> create database [database name];

So we should run the following command to create a database named linux

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

Has it been built? ? It must have been built, because everything is OK :)

Check if there is a Linux database?

Code:

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

mysql>

So how do we delete a database? ?
mysql> drop database [database name];

For example, if we want to delete the linux database we just created, we should use the following command;
mysql> drop database linux;
Query OK, 0 rows affected (0.00 sec)

Yes Hasn't it been deleted? ?

Code:

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

mysql>


5] There are many questions about how to operate a database. It is recommended to read the mysql manual. There's so much in there. If you operate a database, you must first specify a database as the current database. You should use the use command

mysql> use [database];

For example, if I want to specify the linux database as the current database, it should be

mysql> use linux;
Database changed
mysql>



6] How to back up the database? ?

For example, if we want to back up a database named linux that already exists in mysql, we need to use the command mysqldump

The command format is as follows:

[root@linuxsir01 root]# /opt/mysql/bin/mysqldump -uroot -p linux > /root/linux.sql
Enter password: Enter the password of the database here

Through the above command, we need to understand two things. First, the database must be backed up as a database administrator; secondly: the backup destination It is /root, and the backup file name is linux.sql. In fact, the location and file name of the backup are determined according to your own situation. You can choose the file name yourself and arrange the path yourself;

For example, I want to back up the Linux database to /home/beinan. The file name of the database is linuxsir031130.sql, so I should enter the following command.
[root@linuxsir01 root]#/opt/mysql/bin/mysqldump -uroot -p linux > /home/beinan/linuxsir031130.sql
Enter password: Enter the database password of the database administrator root here

This way we get to The backup file linuxsir031130.sql of the database named linux in mysql can be found in the /home/beinan directory

To sum up, we must learn to be flexible when studying. :):)

5] How to import the backed up database into the database?

First we still need to operate the above processes, such as adding a database administrator (if you have not added a mysql database administrator), creating a database, etc.

For example, if we want to import the backup of linuxsir031130.sql in the directory /home/beinan into a database named linux, we should do the following;

[root@linuxsir01 root]# /opt/mysql/bin/mysql -uroot -p linux < /home/beinan/linuxsir031130.sql
Enter password: Enter the password here

If the machine is good and the database is small, it will only take a few minutes.

6] Some other commonly used mysql commands;

View status
mysql> show status;

View process

Code:

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>

To view the table, you should first specify a database as the current database; for example, a database named linux;

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

mysql>


7] A little supplement to the common commands of mysql database;


Several commonly used mysql related management commands

mysql command: basic text, Display and use the mysql database. The usage has been briefly mentioned before; such as login, etc.

mysqladmin command, a command used to create and maintain mysql database, has been briefly mentioned before;

isamchk is used to repair, check and optimize database files with .ism suffix;

mysqldump is used to back up the database, as mentioned earlier It has been briefly explained;


myisamchk is used to repair the database file with the .myi suffix;

For example, if we want to check whether there is a problem with the .myi database table of the database named linux, we should use the following command;

To use mysqld Server stop
[root@linuxsir01 root]# /opt/mysql/share/mysql.server stop

Then execute
[root@linuxsir01 root]# /opt/mysql/bin/myisamchk /opt/mysql/var/linux /*.MYI

The above command means to check all .myi files. The database directory is in the /opt/mysql/var/linux/ directory

If there is a problem, you should use the -r parameter to fix it
[root @linuxsir01 root]# /opt/mysql/bin/myisamchk -r /opt/mysql/var/linux/*.MYI

6]mysqlshow command: Display the database and table selected by the user
[root@linuxsir01 root]# / opt/mysql/bin/mysqlshow -uroot -p [database name]

For example, if I want to view the database named linux; it should be:

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

The above is the basic usage of mysql under Linux. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


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 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)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

See all articles