安装完数据库后,先得启动数据库,即
systemctl start mysqld
再去查找数据库的初始密码,(yum 安装)
cat /var/log/mysqld.log |grep password|grep localhost:|awk -F"localhost:" '{print $NF}'
执行上面语句就会得到初始密码;mysql -uroot -p'密码'
即可登录数据库
常见问题
ERROR 2002 (HY000): Can’t connect to local MySQL server
through socket ‘/var/lib/mysql/mysql.sock’ (2)
然后去找这个文件,发现/var/lib/mysql/路径下没有mysql.sock文件。而该文件存在于/tmp/mysql.sock !
解决方法
(1)修改my.cnf
文件
vim /etc/my.cnf
[mysqld]
socket = /tmp/mysql.sock
(2)软链
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
解决方法
update user set authentication_string= password('new-password') where user = 'root' ;
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client
that supports expired passwords.
解决方法
在配置文件中添加skip-grant-tables
语句,重新启动数据库即可实现无密登录。
# mysqladmin -uroot -p'123' password 'new_password' //123为旧密码
mysql> update mysql.user set authentication_string=password('new-passwd!') where user='root' and host='localhost';
上面方法将会在后面的版本remove,使用下面方法
SET PASSWORD='new_password'; 直接设置密码
mysql> SET PASSWORD FOR user3@’localhost’='new_password';
UPDATE mysql.user SET authentication_string=password(‘new_password’) WHERE user=’user3’ AND host=’localhost’;
mysql> SET password=password('new_password');
mysql> select * from mysql.user\G
mysql> alter user 'wing'@'localhost' identified by 'new-passwd';
mysql> SHOW VARIABLES LIKE 'validate_password%';
参数解释:
1).validate_password_dictionary_file
指定密码验证的文件路径;
2).validate_password_length
密码最小长度
3).validate_password_mixed_case_count
密码至少要包含的小写字母个数和大写字母个数;
4).validate_password_number_count
密码至少要包含的数字个数
5).validate_password_policy
密码强度检查等级,对应等级为:0/LOW、1/MEDIUM、2/STRONG,默认为1
0/LOW
:只检查长度;
1/MEDIUM
:检查长度、数字、大小写、特殊字符;
2/STRONG
:检查长度、数字、大小写、特殊字符字典文件。
6).validate_password_special_char_count
密码至少要包含的特殊字符数
# mysqld --skip-grant-tables --user=mysql & //跳过数据库权限验证
# mysql
mysql> UPDATE mysql.user SET authentication_string=password('new_password')
WHERE user='root' AND host='localhost';
mysql> FLUSH PRIVILEGES; 刷新授权表
ERROR You must reset your password using ALTER USER statement before executing this statement
解决方案
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-passwd';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
报错原因:密码强度不够
解决方法:(该账号为测试账号,所以采用降低密码策略强度)
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=4;
SHOW VARIABLES LIKE 'validate_password%';
±-----------------------------------------------------±------+
| Variable_name | Value |
±-----------------------------------------------------±------+
| validate_password_dictionary_file | |
| validate_password_length | 4 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
±----------------------------------------------------±---- --+
6 rows in set (0.00 sec)
再次创建用户,成功