您的位置:首页 > 数据库 > MySQL

mysql使用问题记录

2017-02-10 12:09 204 查看
Mysql Access denied for user 'root'

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)



解决方法: 更改root密码

原密码为空,更改root密码

# service mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking


然后登陆mysql

sudo mysql -u root,设置密码

mysql> use mysql;
mysql> update user set password=password('新密码') where user='root';
mysql> flush privileges;
mysql> quit


重启数据库再次登陆即可

# service mysql restart
Enter password: <输入新设的密码>





增加一个数据库用户

mysql> insert into mysql.user (Host,User,Password) VALUES('%','your username',PASSWORD('your password'));
mysql> flush privileges;


创建数据库

create database DatabaseName;
grant select,insert,update,delete,create,drop,alter on DatabaseName.* to username@localhost identified by 'password';


把数据库权限全部授予指定用户

grant all privileges on DatabaseName.* to 'username'@'%';


远程登陆

授权用户root使用密码123456从任意主机连接到mysql服务器:

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;


授权用户test使用密码123456,从指定ip为123.4.5.6的主机连接到mysql服务器:

grant all privileges on *.* to 'test'@'123.4.5.6' identified by '123456' with grant option;
flush privileges;


然后我们查询用户情况

mysql> use mysql;
mysql> select host,user from user;


如果是类似与这样的输出,test前有%,代表从任意主机登陆

+----------------+------+
| host           | user |
+----------------+------+
| %              | test |
| 123.4.5.6      | test |
| localhost      | root |
+----------------+------+


比如我想让root从任意ip远程访问,也可以在这里直接修改

mysql> update user set host = '%' where user = 'root';


备份mysql数据库

mysqldump -u 用户名 -p 密码 数据库名 > back.sql  //备份指定数据库
mysqldump --all-databases > bak.sql    //备份所有数据库


还原mysql数据库

mysql -u 用户名 -p 密码 数据库名 < bak.sql
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: