Common commands for mysql installation and addition, deletion, modification and query

mysql database

1. Overview of mysql

1. MySQL is an open source relational database server software. MySQL is a relational database management system developed by the Swedish MySQL AB company and is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System) application software.
MySQL is a relational database management system. A relational database stores data in different tables instead of putting all data in one large warehouse, which increases speed and flexibility.
The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual licensing policy and is divided into community version and commercial version. Due to its small size, fast speed, low total cost of ownership, especially open source, MySQL is generally chosen as the website for the development of small, medium and large websites. database.
*Currently developed and maintained by Oracle Corporation
*Official site: http://ww.mysql.com

2. Features of mysql
* Multi-threading, multi-user
* Based on C/S (client/server) architecture
* Simple and easy to use, fast query speed
* Safe and reliable

Compile and install mysql on centos

(1), Mysql download link: https://downloads.mysql.com/archives/community/
Insert image description here
Insert image description here

(2) Add the mysql user and user group
groupadd mysql && useradd -r -g mysql mysql to mysql
Insert image description here

(3) Create the data directory of the database and grant permissions to mysql
mkdir /data/mysql -p
chown -R mysql:mysql /data/mysql
Insert image description here

(4)、修改mysql的配置文件
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
Insert image description here

(4) Unzip the tar package of mysql, move the package to /usr/local/mysql, and initialize
tar xf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz #Unzip
Insert image description here

mv mysql-8.0.32-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql/bin/
Insert image description here

./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
Insert image description here

(5) Create database shortcut command and start
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql #Add mysql system service
chkconfig -add mysql #Add system service
systemctl start mysql #Start mysql
Insert image description here

ln -s /usr/local/mysql/bin/mysql /usr/bin/ #Create a soft connection for the mysql command
cat /data/mysql/mysql.err #View the root password of mysql
Insert image description here

Check the running status of mysql
Ps -ef |grep mysql
Netstat -natp |grep 3306
Insert image description here

(6) Log in to mysql
Mysql -uroot -p Log in to mysql
WduaE>:kL1P)
Insert image description here

3. Basic commands of mysql

1. Connect and log in to the MySQL operating environment
mysql -u username [-p password]
Insert image description here

Mysql command parameters:
-V #View database version
-h #Specify login database ip address
-P #Specify database port (default is 3306)
-u #Specify login user

2. Mysql backup command mysqldump
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/ #Create mysqldump command soft connection
mysqldump -u [user] -p [password] [library to be backed up] > /Backup to what #mysqldump command format
mysqldump -uroot -p mysql > /home/mysql_bak_2023_0502.sql #Library name
Insert image description here

Mysqldump command parameters:
-u #Specify user
-p #Enter password

Example:
To back up the database, you can create a separate user.
For example, I need to back up the test database.
The steps are as follows:
(1) Create a back user
create user back@'%' identified by '123456';
Insert image description here

(2) Grant the permission to backup the test library
grant process on . to back@'%';
grant process on test.* to back@'%';
Insert image description here

show grants for back@‘%’;
Insert image description here

(3) Back up the test library
mysqldump -uback -p test > /home/666.sql.
Enter the password: 123456
Insert image description here

3. Modify the user password command mysqladmin
mysqladmin -u username -p password “newpwd” #Command format
mysqladmin -u root -p password “123456”
Insert image description here

Mysqladmin command parameters:
-u #Specify user
Password #Specify new password

4. Basic management commands of database

(1) View the database structure

(1) View the database
SHOW DATABASES
Insert image description here

(2) View the data table information in the database
USE database name #switch to the database
SHOW TABLES #view the tables of the database
Insert image description here

(3) Display the structure (fields) of the data table
DESCRIBE [database name.] table name
Insert image description here

(2) Creation and deletion of database

(1) Create a new database
create database database name
Insert image description here

(2) Create a table in the test library.
Use test
CREATE TABLE table name (field definition...)
create table users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name)) ;
Insert image description here

(3) Delete the table
DROP TABLE database name. table name
drop tables test.users;
Insert image description here

(4) Delete the database
DROP DATABASE auth;
drop database test;
Insert image description here

(3) Inserting and querying data records

(1) Insert new data records into the data table.
First create the database and table
create database test; #Create the test library
use test; #Switch to the test library
create table users(user_name CHAR(16)NOT NULL, user_passwd CHAR(48) )DEFAULT '', PRIMARY KEY (user_name)); # Create users table
Insert image description here

INSERT INTO table name (field 1, field 2, ...) VALUES (field 1 value, field 2 value, ...)
insert into users(user_name,user_passwd) values('zhangsan','123456');
insert into users(user_name,user_passwd) values('yzq','123456');
insert into users(user_name,user_passwd) values('wangwu','123456')
Insert image description here

(2) Find data records that meet the conditions from the data table
SELECT field name 1, field name 2... FROM table name WHERE conditional expression
select * from users; #Query all information in the users table
Insert image description here

Select user_name,user_passwd from users where user_name=”lisi”\G; #where condition query, query user_name = lisi
Insert image description here

Select user_name,user_passwd from users where user_passwd = '123456' and user_name = 'yzq'; #Multi-condition query, query user_name =yzq and user_passwd= 123456
Insert image description here

(4) Modify data records

(1) Modify and update data records in the data table
UPDATE table name SET field name 1 = value 1 [, field name 2 = value 2] WHERE conditional expression
UPDATE auth.users SET user_passwd=PASSWORD('666666') WHERE user_name='lisi'; #Modify users' lisi's user_passwd=666666
Insert image description here

(2) Delete wangwu’s information in the users table
DELETE FROM table name WHERE conditional expression
delete from test.users where user_name = 'wangwu';
Insert image description here

5. Optimization of mysql database

(1) Modify the root password
Method 1: Modify the root user's password in the database
alter user 'root'@'localhost' identified by '666666'
flush privileges; #Refresh the permission table
Insert image description here

Method 2:
Enter outside the database:
mysqladmin -u root -p password "123456"
and enter the old password
Insert image description here

(2) For security reasons, delete empty users whose user names and passwords are empty
select user,host,password from mysql.user where user=''; #Query users whose passwords are empty
delete from mysql.user where user= ''; #Delete users with empty passwords

6. Mysql database maintenance database and user permission commands

grant --authorize
revoke --revoke
identified by Set password
(1), set user permissions (when the user does not exist, create a new user) The data usage of version 8.0 is different format
: GRANT permission list ON database name. table name TO user Name@source address [ IDENTIFIED BY 'password' ]

Format description name Parameters and description
Permission list Select (check), insert (insert), update (change), all (all permissions) comma separated all – all permissions
Source address % (match all) can be domain name, IP address, etc.
Password options If omitted, the user password is empty

Taking the MySQL version after 8.0 as an example,
you need to create a user first:
create user test@'%' identified by '123456';
grant all privileges on test.* to test@'%';
Insert image description here

(2) View user permissions
SHOW GRANTS FOR username@source address;
show grants for test@'%';
Insert image description here

(3) Revoke user permissions
revoke permission list ON database name.table name from username@source address
revoke all on test.* from test@'%';
Insert image description here

Guess you like

Origin blog.csdn.net/m0_57207884/article/details/130465728