安装mysql命令

sudo apt-get install mysql-server

安装完毕后,立即初始化密码

sudo mysql -u root # 初次进入终端无需密码ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd'; # 设置本地root密码

设置mysql远程登录

  1. 设置远程登录账户
# mysql终端内执行use mysqlupdate user set host = '%' where user ='root' and host = 'localhost';flushprivileges;
  1. 配置mysql文件开启远程登录
    /etc/mysql/my.cnf
# 修改[mysqld]中的bind-address直接注释 bind-address这一行# bind-address>->---= 127.0.0.1
  1. 重启mysql服务
sudo service mysql restart

如果my.cnf显示这些不要慌,文件中最后两个includedir指向的是配置文件的路径,可以自己去看看mysqld.cnf在哪里再进行配置

我的在第二个/mysql.conf.d

如果报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

解决方案:

sudo service mysql stopsudo usermod -d /var/lib/mysql mysqlsudo service mysql start