当前位置:网站首页>Examples of MySQL account addition, deletion, modification, data import and export commands

Examples of MySQL account addition, deletion, modification, data import and export commands

2022-06-22 16:55:00 MarshalEagle

The following examples are based on mysql The database has been built as follows :

a,school And it includes t_student、t_teacher surface

b,company And it includes t_staff、t_leader

One ,mysql Add, delete, modify and check the account number

1) establish account1 Account and set password , Set it to be accessible remotely and locally school and company All tables in the data have all permissions such as adding, deleting, modifying and querying .

a,

Want the local server to be able to connect to , The following two steps must be carried out : stay user Add two records to the table --------------------------A
mysql>CREATE USER 'account1'@'localhost' IDENTIFIED BY '
mypasswd';
              mysql>CREATE USER 'account1'@'127.0.0.1' IDENTIFIED BY '
mypasswd
';
 
        If you just want the outside ip Access to the , among % Represents all of the
external ip: stay user Add a record to the table --------------------------B     
mysql>CREATE USER 'account1'@'%' IDENTIFIED BY 'mypasswd';

In this case, execute A+B.

b, Assign all operation permissions of all databases to account1 user , And external access ip unlimited

mysql>GRANT ALL PRIVILEGES ON *.* TOaccount1@"%" IDENTIFIED BY "mypasswd";
mysql>flush privileges;
  

perhaps

mysql>GRANT ALL PRIVILEGES ON school.* TOaccount1@"%" IDENTIFIED BY "mypasswd";

mysql>GRANT ALL PRIVILEGES ON company.* TOaccount1@"%" IDENTIFIED BY "mypasswd";
mysql>flush privileges;

2) establish account2 Account and set password , At the same time, set its only Remote and local access school All tables in the data are merged only Have the authority to add, modify and query .

a,

Want the local server to be able to connect to , The following two steps must be carried out : stay user Add two records to the table --------------------------A
mysql>CREATE USER 'account2'@'localhost' IDENTIFIED BY '
mypasswd';
              mysql>CREATE USER 'account
2'@'127.0.0.1' IDENTIFIED BY 'mypasswd
';
 
        If you just want the outside ip Access to the , among % Represents all of the
external ip: stay user Add a record to the table --------------------------B     
mysql>CREATE USER 'account2'@'%' IDENTIFIED BY 'mypasswd';

In this case, execute A+B.

b, take school Database Add, change and check The operation permission is assigned to account2 user , And external access ip unlimited

mysql>GRANT select,update,insert PRIVILEGES ON school.* [email protected]"%" IDENTIFIED BY "mypasswd";

mysql>flush privileges;

3) Users' deletion, modification and query

a, see account2 User permissions

mysql>show grants for 'account2'@'%';

or

mysql>select *from mysql.user where user='account2';

b, modify account2 User password

mysql>update user set password=password('new_password') where user='account2';
mysql>flush privileges;

or

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

Be careful : If root Password forgotten or lost , Kill them first mysqld And then

#mysqld_safe --skip-grant-tables &

#mysql -u root mysql

  mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
  mysql> FLUSH PRIVILEGES;


c, Delete account2 user

mysql>delete from mysql.user where user = 'account2';


Two ,mysql Data export import

a, export :

1) export school Database data to (/home/file/school.sql)

mysqldump -u user name -p password Database name > Database name .sql

#cd /home/file
#mysqldump -uroot -pmypasswd school > school.sql

2) Export only school Table structure of database
mysqldump -u user name -p password -d Database name > Database name .sql
 #mysqldump -uroot -p -d school > school.sql

  3) export school In the database t_student Table data
#mysqldump -uroot -pmypasswd school t_student > /home/file/t_access_log.sql

  4) export school In the database t_student Part of the table data ( Older than 16 Less than 18)
#mysqldump -uroot -pmypasswd school t_student --where="age>'16' and age<'18'" > /home/t_student.sql


b, Import :

mysql>create database school;( Get into mysql The command line interface establishes the database school)

1) Set the code to utf8, Import school.sql File to newly created school database

mysql>use school;

mysql>set names utf8;

mysql>source /home/file/school.sql

    2) Import school.sql File to newly created school database

#mysql -uroot -p school < /home/file/school.sql( Enter the password )




原网站

版权声明
本文为[MarshalEagle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221525110251.html