注:默认环境Linux,在线或离线安装mysql
MySQLfor Linux安装完毕后默认没有密码,安装完毕后增加密码的重要性就不言而喻了。
1、检查MySQL是否安装成功
第一次连接时,root是无密码的,在SSH命令窗口输入命令:mysql
[root@test1 local]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 4.0.16-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. |
出现了“mysql>”提示符,表示MySQL安装成功。
使用命令:exit 退出mysql的命令模式。
mysql>exit [root@test1 local]# |
2、初始化root密码
使用命令:usr/bin/mysqladmin -uroot password 'new-password'
给root加个密码tyxj456cg,使用以下命令:
[root@test1 local]# /usr/bin/mysqladmin -u root password 123456 |
注:在初始化后,不能再次进行初始化密码,只能对密码进行更改
3、测试是否设置成功
1)不用密码登录
[root@test1 local]# mysql ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO) |
显示错误,说明密码已经修改。
2)用修改后的密码登录
[root@test1 local]# mysql -u root -p Enter password: (输入修改后的密码tyxj456cg) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 to server version: 4.0.16-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> |
成功!
使用命令:exit 退出mysql的命令模式。
mysql>exit; [root@test1 local]# |
注:
异常一:若在设置好密码后,依然无需密码登陆,且无法使用mysql表(问题提示如下),解决方法参见:
异常二:若清理空的用户数据(推荐使用方法一);若登录密码忘记或错误,强制修改密码(推荐使用方法二)
附录一:2、ERROR…: Access denied for user ''@'localhost' to database 'mysql'