总是提示 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
但是我查看/var/lib/目录下并没有mysqlmysqllinux

解决方案 »

  1.   

    Linux下安装mysql5.0已编译的安装包步骤如下,你的问题是没有连接到数据库,启动它的命令是bin/mysqld_safe --user=mysql --socket=/tmp/mysql.sock --port=3306 &
    下面提供所有安装步骤,希望对你有帮助。
    1.下载mysql-5.0.51a-linux-i686-glibc23.tar.gz,下载地址为http://download.unix-center.net/mysql/mysql-5.0.51a-linux-i686-glibc23.tar.gz
    2.把下载的文件放在你的主目录,在终端输入ls,如下:
    [root@localhost mysql]# ls
    Desktop  Documents  Download  Music  mysql-5.0.51a-linux-i686-glibc23.tar.gz  Pictures  Public  Templates  Videos
    3.然后用tar -zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz命令解压到当前目录,如下:
    [root@localhost mysql]# tar -zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz
    [root@localhost mysql]# ls
    Desktop    Download  mysql-5.0.51a-linux-i686-glibc23         Pictures  Templates
    Documents  Music     mysql-5.0.51a-linux-i686-glibc23.tar.gz  Public    Videos
    4.因为用的是源代码,所以要把文件copy到安装目录/usr/local/下,才能运行!
    [root@localhost mysql]# cp –rf mysql-5.0.51a-linux-i686-glibc23 /usr/local/ 
    5.查看系统有没有安装过mysql,查找rpm,如下:
    [root@localhost mysql]# rpm –qa | grep mysql 
    有的话一个一个删除掉,用rpm -e命令,然后查找一下残留的文件:
    [root@localhost mysql]# cd /
    [root@localhost /]# find / -name mysql
    如果查找到rm -rf filename删除.
    [root@localhost /]# find / -name my.cnf
    如果查找到删除,一般my.cnf是在/etc/my.cnf这里.
    6.链接到mysql,如下:
    [root@localhost /]# cd /home/mysql
    [root@localhost mysql]# ln -s mysql-5.0.51a-linux-i686-glibc23 /usr/local/mysql
    7.初始化mysql表格,安装 mysql 默认数据库,如下:
    [root@localhost mysql]# cd /usr/local/mysql
    [root@localhost mysql]# scripts/mysql_install_db --user=mysql
    Installing MySQL system tables...
    OK
    Filling help tables...
    OKTo start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    ./bin/mysqladmin -u root password 'new-password'
    ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'Alternatively you can run:
    ./bin/mysql_secure_installationwhich will also give you the option of removing the test
    databases and anonymous user created by default.  This is
    strongly recommended for production servers.See the manual for more instructions.You can start the MySQL daemon with:
    cd . ; ./bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.pl
    cd mysql-test ; perl mysql-test-run.plPlease report any problems with the ./bin/mysqlbug script!The latest information about MySQL is available on the web at
    http://www.mysql.com
    Support MySQL by buying support/licenses at http://shop.mysql.com
    8.修改目录权限,如下:
    [root@localhost mysql]# cd /usr/local
    [root@localhost local]# chgrp -R mysql mysql-5.0.51a-linux-i686-glibc23
    [root@localhost local]# chgrp -R mysql mysql
    [root@localhost local]# chown -R mysql mysql-5.0.51a-linux-i686-glibc23/data
    [root@localhost local]# chown -R mysql mysql/data
    [root@localhost local]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
    9.启动mysql_safe,如下:
    [root@localhost local]# bin/mysqld_safe --user=mysql --socket=/tmp/mysql.sock --port=3306 &
    [1] 26860
    [root@localhost local]# nohup: ignoring input and redirecting stderr to stdout
    Starting mysqld daemon with databases from /usr/local/mysql/data
    查看一下 3308 端口是否已经在监听:
    netstat -anp|grep LISTEN
    10.添加到系统自启动,如下:
    [root@localhost local]# cp mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld 
    [root@localhost local]# chkconfig --add mysqld 
    11.启动mysql服务进程,如下:
    [root@localhost local]# /etc/init.d/mysqld start
    Starting MySQL                                             [  OK  ]
    12.给 root 指定密码为"python",host为localhost ,如下:
    [root@localhost local]# bin/mysqladmin --socket=/tmp/mysql.sock --port=3306 -u root -h localhost password 'python'
    13.登录root,并给root授权!如下所示:
    [root@localhost local]# bin/mysql --port=3306 -u root -p -S /tmp/mysql.sock
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.0.51a MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'python';
    Query OK, 0 rows affected (0.54 sec)#[给使用localhost连接的root用户以所有权限]mysql>flush privileges;
    Query OK, 0 rows affected (0.26 sec)#[刷新用户权限列表,更新权限]
    mysql> quit
    Bye
    14.新建一个用户,如下:
    //登录MYSQL
    [root@localhost local]# bin/mysql --port=3306 -u root -p -S /tmp/mysql.sock
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.0.51a MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    //使用mysql数据库
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -ADatabase changed
    //创建用户
    mysql> insert into mysql.user(Host,User,Password) values("localhost","django",password("django"));
    Query OK, 1 row affected, 3 warnings (0.60 sec)
    //刷新系统权限表
    mysql> flush privileges;
    Query OK, 0 rows affected (0.08 sec)
    这样就创建了一个名为:django  密码为:django  的用户。
    //然后登录一下
    mysql> exit;
    Bye
    [root@localhost local]# bin/mysql --port=3306 -u django -p -S /tmp/mysql.sock
    Enter password: //输入密码
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.0.51a MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> //登录成功!
    mysql> exit;
    Bye
    15.给新建的用户账户授权,如下:
    //登录MYSQL(有ROOT权限)。我里我以ROOT身份登录.
    [root@localhost local]# bin/mysql --port=3306 -u root -p -S /tmp/mysql.sock
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.0.51a MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql>
    //首先为用户创建一个数据库(django_demo)
    mysql> create database django_demo;
    Query OK, 1 row affected (0.41 sec)
    //授权django用户拥有django_demo数据库的所有权限。
    mysql> grant all privileges on django_demo.* to 'django'@'localhost' identified by 'django';
    Query OK, 0 rows affected (0.13 sec)
    //刷新系统权限表
    mysql> flush privileges;
    Query OK, 0 rows affected (0.09 sec)
      

  2.   

    检查一下进程中是否存在 mysqld 
      

  3.   

    mysql服务怎么启动?都试过了。还是不行阿。提示 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)。但是/var/lib/mysql/没有mysql.sock只有/var/lib/mysql/mysql/,但是还是没有mysql.sock。先谢谢你们了~