1. 程式人生 > >ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

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

1:下載

[[email protected] /]# tar -xzvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ 1 3修改檔案

[[email protected] local]# mv mysql-5.7.19-linux-glibc2.12-x86_64 mysql 1 4:檢查庫檔案是否有刪除,若有便刪除(linux系統自帶的)

[[email protected] mysql]# rpm -qa | grep mysql

1 刪除 1 [[email protected] mysql]# rm -e –-nodeps mysql-libs-5.1.52.x86_64 1 5:檢查mysql組和使用者是否存在,如無建立

  [[email protected] ~]# cat /etc/group | grep mysql
[[email protected] ~]# cat /etc/passwd |grep mysql

1 2 建立

[[email protected] ~]#groupadd mysql
[[email protected] ~]#useradd -r -g mysql mysql
    //useradd -r引數表示mysql使用者是系統使用者,不可用於登入系統

1 2 3 6:在mysql下新增data目錄

  [[email protected]
mysql]# mkdir data

1 7:更改mysql目錄下所有的目錄及資料夾所屬組合使用者

[[email protected] mysql]# cd /usr/local/ [[email protected] local]# chown -R mysql mysql/ [[email protected] local]# chgrp -R mysql mysql/ [[email protected] local]# cd mysql/ [[email protected] mysql]# ls -l total 40 drwxr-xr-x. 2 mysql mysql 4096 Aug 31 16:45 bin -rw-r–r--. 1 mysql mysql 17987 Jun 22 22:13 COPYING drwxr-xr-x. 2 mysql mysql 6 Aug 31 16:48 data drwxr-xr-x. 2 mysql mysql 52 Aug 31 16:45 docs drwxr-xr-x. 3 mysql mysql 4096 Aug 31 16:44 include drwxr-xr-x. 5 mysql mysql 4096 Aug 31 16:45 lib drwxr-xr-x. 4 mysql mysql 28 Aug 31 16:45 man -rw-r–r--. 1 mysql mysql 2478 Jun 22 22:13 README drwxr-xr-x. 28 mysql mysql 4096 Aug 31 16:45 share drwxr-xr-x. 2 mysql mysql 86 Aug 31 16:45 support-files 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 8:安裝和初始化資料庫 安裝

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/–datadir=/usr/local/mysql/data/

2017-08-31T08:50:23.910440Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-08-31T08:50:23.910635Z 0 [ERROR] Can’t find error-message file ‘/usr/local/mysql/–datadir=/usr/local/mysql/data/share/errmsg.sys’. Check error-message file location and ‘lc-messages-dir’ con figuration directive.2017-08-31T08:50:24.709286Z 0 [Warning] InnoDB: New log files created, LSN=45790 2017-08-31T08:50:24.767540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-08-31T08:50:24.892629Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6e083b8f-8e29-11e7-88b1- 005056b427be.2017-08-31T08:50:24.895674Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened. 2017-08-31T08:50:24.896645Z 1 [Note] A temporary password is generated for [email protected]: gFamcspKm2+u 1 2 3 4 5 6 7 8 9 10 報錯[ERROR] Can’t find error-message file ‘/usr/local/mysql/–datadir=/usr/local/mysql/data/share/errmsg.sys’. Check error-message file location and ‘lc-messages-dir’ con

解決

[[email protected] bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US 2017-08-31T09:00:54.941514Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-08-31T09:00:56.364312Z 0 [Warning] InnoDB: New log files created, LSN=45790 2017-08-31T09:00:56.602211Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-08-31T09:00:56.668145Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e69986d2-8e2a-11e7-a335- 005056b427be.2017-08-31T09:00:56.671464Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened. 2017-08-31T09:00:56.672453Z 1 [Note] A temporary password is generated for [email protected]: qfuqvCsHb2!. 1 2 3 4 5 6 7 9配置my.cnf 接下來進入/usr/local/mysql/support-files/目錄下 檢視是否存在my-default.cnf檔案,如果存在直接copy到/etc/my.cnf檔案中

[[email protected] mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf

1 如果不存在my-default.cnf檔案,則在/etc/目錄下建立my.cnf,並寫入以下內容

#[mysql] #basedir=/usr/local/mysql/ #datadir=/usr/local/mysql/data/ 1 2 3 10啟動服務

[[email protected] mysql]# cd bin/ [[email protected] bin]# ./mysqld_safe --user=mysql & [2] 10436 [[email protected] bin]# Logging to ‘/var/log/mysql/mysql.log’. 2017-08-31T09:52:15.806633Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 2017-08-31T09:52:16.292949Z mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended 1 2 3 4 5 6 7 11將mysqld服務加入開機自啟動項。 將{mysql}/ support-files/mysql.server 拷貝為/etc/init.d/mysql並設定執行許可權,這樣就可以使用service mysql命令啟動/停止服務, 否則就只能使用{mysql}/bin/mysqld_safe &命令來啟動服務 還需要把mysql.server中basedir的相關路徑,改為自定義的路徑,預設路徑是/usr/local/mysql

[[email protected] support-files]# cp mysql.server /etc/init.d/mysql [[email protected] support-files]# chmod +x /etc/init.d/mysql – 把mysql註冊為開機啟動的服務 [[email protected] support-files]# chkconfig --add mysql – 檢視是否新增成功 [[email protected] support-files]# chkconfig --list mysql Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration.

  If you want to list systemd services use 'systemctl list-unit-files'.
  To see services enabled on particular target use
  'systemctl list-dependencies [target]'.

mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 12啟動服務

[[email protected] bin]# service mysql start Starting MySQL.Logging to ‘/var/log/mysql/mysql.log’. ERROR! The server quit without updating PID file (/var/lib/mysql/dbserver.pid). 1 2 3 4 解決

[[email protected] mysql]# rm /etc/my.cnf rm: remove regular file ‘/etc/my.cnf’? y [[email protected] mysql]# /etc/init.d/mysql start Starting MySQL.Logging to ‘/usr/local/mysql/data/dbserver.err’. SUCCESS! [[email protected] mysql]# service mysql start Starting MySQL SUCCESS! 1 2 3 4 5 6 7 8 13登入mysql

[[email protected] bin]# ./mysql -u root -p 密碼是第八步產生的密碼 1 2 14設定密碼

mysql> set password=password(“root”); Query OK, 0 rows affected, 1 warning (0.00 sec) 注意不要使用單引號,為什麼?你自己試試就知道了 1 2 3 15設定遠端登入許可權

mysql> grant all privileges on . to’root’ @’%’ identified by ‘root’; Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges; Query OK, 0 rows affected (0.06 sec)

mysql> quit Bye