linux安装配置mysql

系统Ubuntu 18.04.2 LTS

  1. 安装

    sudo apt-get install mysql-server
    
  2. 安装后设置密码

    sudo cat /etc/mysql/debian.cnf  # 获取默认密码
    

    linux安装配置mysql_第1张图片

    mysql -u debian-sys-maint -p  # 使用默认的账号密码登陆:
    
    # 修改root密码为123456;
    update mysql.user set authentication_string=password('123456') where user='root';	
    
    刷新数据库并重启服务
    flush privileges;
    exit;
    service mysql restart;
    

    如果刚设置的root账号还是无法登陆:
    在这里插入图片描述
    这个问题可能是由于Ubuntu默认安装的mysql使用的plugin是auth_socket:
    linux安装配置mysql_第2张图片
    需要修改plugin为mysql_native_password即可:

    mysql> update user set plugin='mysql_native_password' where User='root';
    
    # 刷新数据库并重启服务
    mysql> flush privileges;
    mysql> exit;
    service mysql restart;
    
  3. 开远程登陆权限

    # 登陆mysql,给root添加远程权限
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
    
    mysql> use mysql;
    
    设置后查询:
    mysql> select  User,authentication_string,Host from user;
    +------------------+-------------------------------------------+-----------+
    | User             | authentication_string                     | Host      |
    +------------------+-------------------------------------------+-----------+
    | root             |                                           | localhost |
    | mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
    | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
    | debian-sys-maint | *EC48E50E78101AA4E9A3C6BDC853EF1FAC334D07 | localhost |
    | root             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | %         |
    +------------------+-------------------------------------------+-----------+
    

    修改配置文件:

    sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
    注:有的可能在 /etc/mysql/my.cnf
    找到bind-address = 127.0.0.1
    bind-address = 127.0.0.1 修改为
    #bind-address = 127.0.0.1(注释掉)
    

    重启数据库服务: service mysql restart;

你可能感兴趣的:(数据库,Linux,MySQL)