mysql create remote user and authorize
Today, you need to test the system functions locally, because there is no local database, you need to connect to the remote database in the program;
First log in to the remote server with ssh, and use root to connect to the database to see the situation;
mysql> select Host,User,Password from mysql.user; +----------------+------------------+-------------------------------------------+ | Host | User | Password | +----------------+------------------+-------------------------------------------+ | localhost | root | *836E233974EBE6EA32F95F890A91363F8427F78B | | iz94926clkiz | root | *836E233974EBE6EA32F95F890A91363F8427F78B | | 127.0.0.1 | root | *836E233974EBE6EA32F95F890A91363F8427F78B | | ::1 | root | *836E233974EBE6EA32F95F890A91363F8427F78B | | localhost | debian-sys-maint | *1460ED3535ABDBB887F9E5F57F40A2354610CDF3 | +----------------+------------------+-------------------------------------------+ 5 rows in set (0.00 sec)
There are a total of 5 mysql accounts, as you can see from the Host column, these accounts only support the local connection of the server, now let's create a remote user;
create user test identified by '123456';
+----------------+------------------+-------------------------------------------+
| Host | User | Password | +----------------+------------------+-------------------------------------------+ | localhost | root | *836E283974EBE6EA32F95F890A91363F8427F78B | | iz949s6clkiz | root | *836E283974EBE6EA32F95F890A91363F8427F78B | | 127.0.0.1 | root | *836E283974EBE6EA32F95F890A91363F8427F78B | | ::1 | root | *836E283974EBE6EA32F95F890A91363F8427F78B | | localhost | debian-sys-maint | *1460ED35E5ABDBB887F9E5F57F40A2354610CDF3 | | % | test | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----------------+------------------+-------------------------------------------+ 6 rows in set (0.00 sec)
After the creation is completed, it is found that there is still no permission in the program connection. We just created a user and have not assigned permissions to this user;
grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option;
all represents accepting all operations, such as select, insert, delete....; *.* represents all tables under all libraries; % represents that the user is allowed to log in from anywhere; for the sake of security, this % can be replaced with what you allow ip address;
Then refresh the mysql user permission related table;
flush privileges ;
I thought it was over, wait, why can't the program connect, or access deny;
Isn't the port 3306, open the mysql configuration file, it is the default 3306, then look down and find a key place;
bind-address = 127.0.0.1
It turns out that mysql is bound to the local ip by default and does not accept other sources; comment it out, restart mysql and everything is OK;
There are also two common operations;
Modify the specified user password
update mysql.user set password=password('新密码') where User="test" and Host="localhost";
delete users
delete from user where User='test' and Host='localhost';