apt-get install mysql-server
systemctl start mysql
systemctl status mysql
如下图所示,mysql服务正在运行
在第一次安装MySQL时,root用户没有密码。为了保护数据库的安全,必须设置一个root密码。使用以下命令登录到MySQL控制台
mysql -u root
此时登录到MySQL控制台中
此时输入
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; # 将password替换成你要设置的密码
systemctl stop mysql
apt-get remove --purge mysql-server mysql-client mysql-common
有时候忘记root账户密码,如何重置?
systemctl stop mysql
vim /etc/mysql/my.cnf
在文件中添加如下字串,并保存
[mysqld]
skip-grant-tables
systemctl start mysql
mysql -u root
注意,此时不能直接修改,必须先设置为空,不然会报错
使用 mysql 数据表
use mysql;
将密码置为空
update user set authentication_string='' where user='root';
退出 mysql
quit;
vim /etc/mysql/my.cnf
[mysqld]
skip-grant-tables
systemctl restart mysql
登入已经置空密码的 mysql ,输入密码行用 enter 键入
mysql -u root -p
更改密码( 123456 为笔者重设的密码)
alter user 'root'@'localhost' identified by '123456';
退出 mysql
quit;
输入新密码,再次登陆
mysql -u root -p
登录成功
登录mysql控制台,添加账号时报错(ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation)
systemctl stop mysql
vim /etc/mysql/my.cnf
在文件中添加如下字串,并保存
[mysqld]
skip-grant-tables
systemctl start mysql
mysql -u root
use mysql;
update mysql.user set Grant_priv='Y',Super_priv='Y' where user='root';
flush privileges;
grant all on *.* to 'root'@'localhost';
quit;
vim /etc/mysql/my.cnf
[mysqld]
skip-grant-tables
systemctl restart mysql
mysql -u root -p
输入密码后,进入mysql控制台
use mysql;
create user '用户名' identified by '密码';
grant all on *.* to '用户名'@'%';
flush privileges;