一.安装MySQL服务器
安装MySQL服务器:
apt-get install mysql-server
对MySQL进行初始化,设置密码:
mysql_secure_installation
注意,这里要设置密码的最低长度为8位,如果你设的密码小于8位,则会提示:Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server
二.设置小于8位的密码
如果想要设置小于8位的密码,则新开一个Linux终端,杀掉上述mysql_secure_installation进程。然后执行:
mysql
SHOW VARIABLES LIKE 'validate_password%';
从上图的“validate_password.length ”可以看出密码长度为8位。
继续执行:
SET GLOBAL validate_password.LENGTH = 4;
SET GLOBAL validate_password.policy = 0;
SET GLOBAL validate_password.mixed_case_count = 0;
SET GLOBAL validate_password.number_count = 0;
SET GLOBAL validate_password.special_char_count = 0;
SET GLOBAL validate_password.check_user_name = 0;
设置MySQL root用户的密码(本机登录密码):
ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
FLUSH PRIVILEGES;
exit
三.设置可以远程连接MySQL数据库
上述设置的密码为本机登录密码,只能实现本机登录MySQL,如果我们执行命令(其中,192.168.0.103为所在电脑ip):
mysql -u root -p -h 192.168.0.103
则会提示:Can't connect to MySQL server on XXX :
要想实现远程连接 MySQL,修改配置文件:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
找到bind-address = 127.0.0.1这一行,改为bind-address = 0.0.0.0
重启MySQL服务:
systemctl restart mysql.service
mysql -u root -p
use mysql
CREATE USER 'root'@'%' IDENTIFIED BY '1234';grant all privileges on *.* to 'root'@'%' ;
FLUSH PRIVILEGES;
exit
然后重新执行命令:
mysql -u root -p -h 192.168.0.103
发现可以远程连接了。
用 navicat,也可以远程连接了。
其它相关资料:《MySQL删除用户(DROP USER)》