Linux -数据库连接,且更改数据库密码
时间:2021-07-01 10:21:17
帮助过:3人阅读
service mysqld status 查看服务状态
mysql is not running mysql服务是停止状态
mysql running mysql服务是启动状态

1.连接数据库
mysql -uroot

备注:输入此命令之后,会报错,这条命令的目录路径为
[root@localhost ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql

但是,这条命令并没有在环境变量path里面。
查看环境变量:echo $PATH

所以,需要加入到环境变量PATH里面。
export PATH=$PATH:/usr/local/mysql/bin/

若想让这条变量永久生效,需要将这条命令放入到 /etc/profile 里,放入到最后一行。


并且使修改的文件生效
source /etc/profile

连接数据库: mysql -uroot

设置数据库密码:
mysqladmin -uroot password ‘ljy5157088‘ ‘ljy5157088’为密码 ‘‘单引号可加可不加,建议加,怕使用的密码中投特殊符号

备注:Warning: Using a password on the command line interface can be insecure.警告:在命令行界面使用密码可能不安全。
这行是报警,而不是报错,只是说在命令行界面使用密码可能不安全。
设置完密码,再进行登陆时,便会被拒绝,命令为 mysql -uroot -p

正确登陆方式如下:

更改数据库密码:(更改已知密码)
mysqladmin -uroot -p‘ljy5157088‘ password ‘5157088‘ 格式为:mysqladmin -uroot -p‘旧密码‘ password ‘新密码’

登陆方式可为:mysql -uroot -p密码

更改数据库密码: (不知道以前的密码)
1. vi /etc/my.cnf
2. 加入 socket=/tmp/mysql.sock #取消密码认证

3. 然后,重启服务 /etc/init.d/mysqld restart

1. 再次登陆时,将不需要密码验证。

- 进入到数据库之后,
- 先要切换用户
- user mysql;

- 执行以下命令 123456为新密码
- 再将之前的修改的配置文件改回去。

11. 然后重启服务 /etc/init.d/mysqld restart
12. 使用新密码登陆即可。
Linux -数据库连接,且更改数据库密码
标签:数据库密码