先更新依赖
sudo apt-get update
安装mysql服务和客户端
sudo apt-get install mysql-client mysql-server
查看mysql服务状态
sudo service mysql status
sudo mysql --version ##查看mysql的版本
sudo service mysql start ##如未启动可以执行该命令启动
sudo systemctl enable mysql ##设置开机启动
sudo mysql_secure_installation ##初始化配置
以root登录mysql
sudo mysql -u root -p
修改root密码
以切换mysql数据库
use mysql;
查看用户名密码
select host, user, Password_reuse_history from mysql.user;
修改root密码,5.7~8.0以下版本
update user set authentication_string = password('123456789'), password_expired = 'N', password_last_changed = now() where user = 'root';
或者
ALTER user 'root'@'localhost' IDENTIFIED BY 'root123456';
# MySQL5.7新特性
update mysql.user set plugin="mysql_native_password";
flush privileges; ##立即生效
quit;
8.0新特性
update user set authentication_string='' where user='root'; ##先将字段置为空
ALTER user 'root'@'localhost' IDENTIFIED BY 'root'; ##修改密码
flush privileges; ##立即生效
quit;
开启远程访问
首先修改文件mysqld.cnf,本人的mysql是通过apt安装的,所以该文件路径为/etc/mysql/mysql.conf.d/mysqld.cnf
vi /etc/mysql/mysql.conf.d/mysqld.cnf
##找到bind-address属性,修改值为 0.0.0.0
创建远程访问root 5.7~8.0以前
use mysql;
CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL ON *.* TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;
如果用户已经存在则修改
use mysql;
update user set host='%' where user='root';
GRANT ALL ON *.* TO 'root'@'%';
alter user 'root'@'%' identified with mysql_native_password by '123456';
FLUSH PRIVILEGES;
8.0新特性
use mysql;
CREATE USER 'mysql.infoschema'@'%' IDENTIFIED BY '123456';
grant all privileges on *.* to 'mysql.infoschema'@'%';
flush privileges;
查看用户名密码以及权限
select host,user from user;
卸载mysql
本文使用的apt安装,则使用apt卸载,执行下面命令
sudo apt-get remove mysql-*
清理残留数据
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P