Ubuntu

ubuntu 16.04 重新安装mysql及遇到的问题

简介:此篇文章源于在Ubuntu 上启动Mysql的报错:`ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'`

此篇文章源于在Ubuntu 上启动Mysql的报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock',以下记录在Ubuntu 16.04上重新安装Mysql的步骤。

卸载Mysql

# 移除与mysql相关的应用
sudo apt-get autoremove mysql* --purge
# 移除apparmor,它是在安装mysql-server时自动安装的依赖包
sudo apt-get remove apparmor  
# 移除mysql的配置文件和相关目录
sudo rm /var/lib/mysql/ -R  
sudo rm /etc/mysql/ -R

卸载期间如果遇到问题,可以一个卸载,再不行就重启电脑,然后重新卸载。

安装Mysql

sudo apt-get install mysql-server

这一步会安装以下包:
apparmor
mysql-client-5.7
mysql-common
mysql-server
mysql-server-5.7
mysql-server-core-5.7
因此无需再手动安装它们,在安装过程中在终端会提示输入root用户的密码,请注意保管,安装完成后就会启动Mysql。

当然安装,可能不会那么顺利,比如以下:

dpkg: error processing package mysql-community-server (--install):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-community-server (= 5.7.17-1ubuntu16.10); however:
  Package mysql-community-server is not configured yet.

dpkg: error processing package mysql-server (--install):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu16) ...

遇到这种问题多是依赖包的安装出现了问题,导致mysql无法启动,无法完成mysql的配置。当在网上搜索的结果无法解决问题时,请记住重启下电脑或者服务器,这是我第三次通过重启服务器解决了mysql的安装问题。

启动和关闭mysql服务器

  • 启动
    service mysql start
    
  • 关闭
    service mysql stop
    
  • 重启
    service mysql reload
    
  • 查看mysql服务器状态

    systemctl status mysql.service
    

  • 查看mysql监听的端口

    netstat -tap | grep mysql
    

    ubuntu@iZ2zees2pdrgl9x5nmwp9dZ:~$ sudo netstat -tap | grep mysql
    [sudo] password for ubuntu:
    tcp        0      0 localhost:mysql         *:*                     LISTEN      6761/mysqld
    

或者

netstat -atnp

ubuntu@iZ2zees2pdrgl9x5nmwp9dZ:~$ sudo netstat -atnp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      787/nginx -g daemon
tcp        0      0 127.0.0.1:5002          0.0.0.0:*               LISTEN      12808/python
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      6761/mysqld
tcp        0      0 0.0.0.0:9999            0.0.0.0:*               LISTEN      1871/server.js
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      787/nginx -g daemon
tcp        0      0 172.17.33.108:53        0.0.0.0:*               LISTEN      740/named
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      900/sshd
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      740/named
tcp        0      0 172.17.33.108:22        111.199.187.119:9569    ESTABLISHED 13054/sshd: ubuntu
tcp        0    296 172.17.33.108:22        111.199.187.119:10777   ESTABLISHED 13220/sshd: ubuntu
tcp        0      0 172.17.33.108:443       123.125.71.45:56773     TIME_WAIT   -
tcp        0      0 127.0.0.1:42632         127.0.0.1:9999          TIME_WAIT   -
tcp        0      0 172.17.33.108:80        92.119.113.26:33342     SYN_RECV    -
tcp        0      0 172.17.33.108:80        92.119.113.26:41371     SYN_RECV    -
tcp        0      0 172.17.33.108:443       92.119.113.26:60997     SYN_RECV    -
tcp        0      0 172.17.33.108:33366     100.100.30.26:80        ESTABLISHED 838/AliYunDun
tcp        0      0 127.0.0.1:42634         127.0.0.1:9999          TIME_WAIT   -
tcp        0      0 172.17.33.108:443       92.119.113.26:61717     SYN_RECV    -

推荐阅读

目录