Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Initial environment setting of MySQL under linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

[1] set the password for root users of MySQL

When MySQL was first installed, its root users were not set a password. First, set the root password for MySQL. [@ more@]

[root@sample ~] # mysql-u root ← logs in to the MySQL server with the root user

Welcome to the MySQL monitor. Commands end with; or g.

Your MySQL connection id is 2 to server version: 4.1.20Type 'help;' or' h' for help. Type 'c'to clear the buffer.

Mysql > select user,host,password from mysql.user; ← to view user information

+-+

| | user | host | password | |

+-+

| | root | localhost | | ← root password is empty |

| | root | sample.centospub.com | | ← root password is empty |

| | sample.centospub.com |

| | localhost |

+-+

4 rows in set (0.00 sec)

Mysql > set password for root@localhost=password ('enter root password here'); ← sets root password

Query OK, 0 rows affected (0.01 sec)

Mysql > set password for root@'sample.centospub.com'=password ('enter root password here'); ← sets root password

Query OK, 0 rows affected (0.01 sec)

Mysql > select user,host,password from mysql.user; ← to view user information

+-+

| | user | host | password | |

+-+

| | root | localhost | 19b68057189b027f | ← root password is set |

| | root | sample.centospub.com | 19b68057189b027f | ← root password is set |

| | sample.centospub.com |

| | localhost |

+-+

4 rows in set (0.01sec)

Mysql > exit ← exits the MySQL server

Bye

-then, test whether the root password is valid.

[root@sample ~] # mysql-u root ← logs in with root with an empty password

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ← has this error message indicating a successful password setting

[root@localhost ~] # mysql-u root-h sample.centospub.com ← logs in with root with an empty password

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ← has this error message indicating a successful password setting

[root@sample ~] # mysql-u root-p ← login with root via password

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g. ← confirms that you can log in successfully with your password

Your MySQL connection id is 5 to server version: 4.1.20Type 'help;' or' h' for help. Type 'c'to clear the buffer.

Mysql > exit

Bye

[root@sample ~] # mysql-u root-h sample.centospub.com-p ← login with root via password

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g. ← confirms that you can log in successfully with your password

Your MySQL connection id is 6 to server version: 4.1.20

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

Mysql > exit ← exits the MySQL server

Bye

+ [2] Delete anonymous users

Just after MySQL is installed, there are users with empty usernames and passwords. This makes it possible for the database server to log in without a password. In order to eliminate the hidden trouble, delete anonymous users.

[root@sample ~] # mysql-u root-p ← login with root via password

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g.

Your MySQL connection id is 7 to server version: 4.1.20Type 'help;' or' h' for help. Type 'c'to clear the buffer.

Mysql > select user,host from mysql.user; ← to view user information

+-- +

| | user | host |

+-- +

| | localhost |

| | root | localhost |

| | sample.centospub.com |

| | root | sample.centospub.com |

+-- +

4 rows in set (0.02 sec)

Mysql > delete from mysql.user where user=''; ← remove anonymous users

Query OK, 2 rows affected (0.17 sec)

Mysql > select user,host from mysql.user; ← to view user information

+-- +

| | user | host |

+-- +

| | root | localhost |

| | root | sample.centospub.com |

+-- +

2 rows in set (0.00 sec)

Mysql > exit ← exits the MySQL server

Bye

+ [3] Delete the test database

After MySQL is installed, there is an empty database named test, which is deleted. Note here that there is also a database called mysql by default, which is used for system management, so please do not delete it.

[root@sample ~] # mysql-u root-p ← login with root via password

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g.

Your MySQL connection id is 8 to server version: 4.1.20

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

Mysql > show databases; ← to view existing databases on the system

+-+

| | Database |

+-+

| | mysql |

| | test |

+-+

2 rows in set (0.02 sec)

Mysql > drop database test; ← deletes an empty database named test

Query OK, 0 rows affected (0.07 sec)

Mysql > show databases; ← to view existing databases on the system

+-+

| | Database |

+-+

| | mysql | ← confirms that the database named test has been deleted and no longer exists |

+-+

1 row in set (0.00 sec)

Mysql > exit ← exits the MySQL server

Bye

+

Test MySQL

Let's test MySQL. This includes creating new users and trying to create databases and data tables with instructions for database operations on relational databases. Here, the new user takes centospub as an example.

[root@sample ~] # mysql-u root-p ← login with root via password

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g.

Your MySQL connection id is 9 to server version: 4.1.20Type 'help;' or' h' for help. Type 'c'to clear the buffer.

Mysql > grant all privileges on test.* to centospub@localhost identified by 'define password here'; ← establishes a user named centospub with full access to the test database

Query OK, 0 rows affected (0.03 sec)

Mysql > select user from mysql.user where user='centospub'; ← confirms the existence of centospub users

+-+

| | user |

+-+

| | centospub | ← confirms that centospub has been established |

+-+

1 row in set (0.01 sec)

Mysql > exit ← exits the MySQL server

Bye

[root@sample ~] # mysql-u centospub-p ← logs in to the MySQL server with the newly created centospub user

Enter password: ← enter your password here

Welcome to the MySQL monitor. Commands end with; or g.

Your MySQL connection id is 10 to server version: 4.1.20

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

Mysql > create database test; ← to set up a database named test

Query OK, 1 row affected (0.00 sec)

Mysql > show databases; ← to view existing databases on the system

+-+

| | Database |

+-+

| | test |

+-+

1 row in set (0.00 sec)

Mysql > use test ← connects to the database

Database changed

Mysql > create table test (num int, name varchar (50)); ← creates tables in the database

Query OK, 0 rows affected (0.03 sec)

Mysql > show tables; ← to view tables that already exist in the database

+-+

| | Tables_in_test |

+-+

| | test |

+-+

1 row in set (0.01 sec)

Mysql > insert into test values (1 ← inserts a value into the table)

Query OK, 1 row affected (0.02 sec)

Mysql > select * from test; ← to view the information of tables in the database

+-+ +

| | num | name |

+-+ +

| | 1 | Hello World! | |

+-+ +

1 row in set (0.00 sec)

Mysql > update test set name='Hello everyone updated; ← updates table information and assigns new values

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

Mysql > select * from test; ← to view the information of tables in the database

+-- +

| | num | name |

+-- +

| | 1 | Hello Everyone! | ← confirms that it has been updated to the new value |

+-- +

1 row in set (0.01 sec)

Mysql > delete from test where num=1; ← deletes values in the table

Query OK, 1 row affected (0.00 sec)

Mysql > select * from test; ← confirm deletion result

Empty set (0.01sec)

Mysql > drop table test; ← delete table

Query OK, 0 rows affected (0.01 sec)

Mysql > show tables; ← view table information

Empty set (0.00 sec) ← confirmation table has been deleted

Mysql > drop database test; ← delete the database named test

Query OK, 0 rows affected (0.01 sec)

Mysql > show databases; ← to view existing databases

Empty set (0.01sec) ← confirms that the test database has been deleted (non-root user relationship here, no database named mysql can be seen)

Mysql > exit ← exits the MySQL server

Bye

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report