一、概括:
1、查看运行日志 vim /var/log/mariadb/mariadb.log
2、修改配置文件 vim /etc/my.cnf
3、修改文件权限 chown mysql.mysql -R /var/lib/mysql/
二、详情:
首先我们尝试启动MySQL,发现没有MySQL进程
root@centos74-tmp sbin]# pwd
/usr/local/mysql/bin
[root@centos74-tmp bin]# service mysqld start
Starting MySQL SUCCESS!
[root@centos74-tmp bin]# pgrep -af mysql
我们查看日志 mariadb.log,发现缺少数据文件
[root@centos74-tmp bin]# vim /var/log/mariadb/mariadb.log
--15T01::.969191Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--15T01::.969334Z [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
--15T01::.969372Z [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.) starting as process ...
--15T01::.976201Z [Note] InnoDB: PUNCH HOLE support available
--15T01::.976243Z [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
--15T01::.976249Z [Note] InnoDB: Uses event mutexes
--15T01::.976267Z [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
--15T01::.976272Z [Note] InnoDB: Compressed tables use zlib 1.2.
--15T01::.976277Z [Note] InnoDB: Using Linux native AIO
--15T01::.976564Z [Note] InnoDB: Number of pools:
--15T01::.976689Z [Note] InnoDB: Using CPU crc32 instructions
--15T01::.978521Z [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = , chunk size = 128M
--15T01::.988017Z [Note] InnoDB: Completed initialization of buffer pool
--15T01::.991091Z [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
--15T01::.001066Z [Note] InnoDB: The first innodb_system data file 'ibdata1' did not exist. A new tablespace will be created!
--15T01::.001400Z [ERROR] InnoDB: Operating system error number in a file operation.
--15T01::.001411Z [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
--15T01::.001417Z [ERROR] InnoDB: Operating system error number in a file operation.
--15T01::.001421Z [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
--15T01::.001427Z [ERROR] InnoDB: Cannot open datafile './ibdata1'
--15T01::.001433Z [ERROR] InnoDB: Could not open or create the system tablespace. If you tried to add new data files to the system tablespace, and it failed here, you should now edit innodb_data_file_path in my.cnf back to what it was, and remove the new ibdata files InnoDB created in this failed attempt. InnoDB only wrote those files full of zeros, but did not yet use them in any way. But be careful: do not remove old data files which contain your precious data!
--15T01::.001439Z [ERROR] InnoDB: InnoDB Database creation was aborted with error Cannot open a file. You may need to delete the ibdata1 file before trying to start up again.
--15T01::.602600Z [ERROR] Plugin 'InnoDB' init function returned error.
--15T01::.602658Z [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
--15T01::.602666Z [ERROR] Failed to initialize builtin plugins.
--15T01::.602670Z [ERROR] Aborting --15T01::.602679Z [Note] Binlog end
--15T01::.603033Z [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
通过配置文件 my.cnf 查看数据文件 ibdata1 路径 ,发现实际文件不在
[root@centos74-tmp mysql]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@centos74-tmp mysql]# cd /var/lib/mysql/data/ibdata1
-bash: cd: /var/lib/mysql/data/ibdata1: No such file or directory
通过 find 命令找到数据文件 ibdata1 实际目录,并修改配置文件 my.cnf
[root@centos74-tmp mysql]# find / -name ibdata*
/usr/local/mysql/data/ibdata1
[root@centos74-tmp mysql]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data/
socket=/var/lib/mysql/mysql.sock
再次尝试启动MySQL,发现端口一会就没了
[root@centos74-tmp mysql]# service mysqld start
Starting MySQL SUCCESS!
[root@centos74-tmp bin]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::4118 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
[root@centos74-tmp bin]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
tcp6 0 0 :::4118 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
再次查看MySQL日志,发现不能创建 mysql.sock.lock 文件
--15T01::.178424Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--15T01::.178562Z [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
--15T01::.178599Z [Note] /usr/local/mysql/bin/mysqld (mysqld 5.7.) starting as process ...
--15T01::.184805Z [Note] InnoDB: PUNCH HOLE support available
--15T01::.184848Z [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
--15T01::.184854Z [Note] InnoDB: Uses event mutexes
--15T01::.184860Z [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
--15T01::.184864Z [Note] InnoDB: Compressed tables use zlib 1.2.
--15T01::.184869Z [Note] InnoDB: Using Linux native AIO
--15T01::.185282Z [Note] InnoDB: Number of pools:
--15T01::.185458Z [Note] InnoDB: Using CPU crc32 instructions
--15T01::.187568Z [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = , chunk size = 128M
--15T01::.197993Z [Note] InnoDB: Completed initialization of buffer pool
--15T01::.202453Z [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
--15T01::.330706Z [Note] InnoDB: Highest supported file format is Barracuda.
--15T01::.338353Z [Note] InnoDB: Log scan progressed past the checkpoint lsn
--15T01::.338438Z [Note] InnoDB: Doing recovery: scanned up to log sequence number
--15T01::.338470Z [Note] InnoDB: Database was not shutdown normally!
--15T01::.338490Z [Note] InnoDB: Starting crash recovery.
--15T01::.702763Z [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
--15T01::.702868Z [Note] InnoDB: Creating shared tablespace for temporary tables
--15T01::.702998Z [Note] InnoDB: Setting file './ibtmp1' size to MB. Physically writing the file full; Please wait ...
--15T01::.831707Z [Note] InnoDB: File './ibtmp1' size is now MB.
--15T01::.836313Z [Note] InnoDB: redo rollback segment(s) found. redo rollback segment(s) are active.
--15T01::.836353Z [Note] InnoDB: non-redo rollback segment(s) are active.
--15T01::.838015Z [Note] InnoDB: Waiting for purge to start
--15T01::.888404Z [Note] InnoDB: 5.7. started; log sequence number
--15T01::.890016Z [Note] InnoDB: Loading buffer pool(s) from /usr/local/mysql/data/ib_buffer_pool
--15T01::.916045Z [Note] Plugin 'FEDERATED' is disabled.
--15T01::.964452Z [Note] InnoDB: Buffer pool(s) load completed at ::
--15T01::.009629Z [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
--15T01::.009838Z [Warning] Failed to set up SSL because of the following SSL library error: SSL_CTX_set_default_verify_paths failed
--15T01::.010380Z [Note] Server hostname (bind-address): '*'; port:
--15T01::.010529Z [Note] IPv6 is available.
--15T01::.010552Z [Note] - '::' resolves to '::';
--15T01::.010588Z [Note] Server socket created on IP: '::'.
--15T01::.010733Z [ERROR] Could not create unix socket lock file /var/lib/mysql/mysql.sock.lock.
--15T01::.010746Z [ERROR] Unable to setup unix socket lock file.
--15T01::.010755Z [ERROR] Aborting
查看文件夹权限,发现属主为root,修改为mysql
[root@centos74-tmp mysql]# ll /var/lib/
total 4
……
drwxr-xr-x 2 root root 6 Jun 15 09:44 mysql
……
[root@centos74-tmp mysql]# chown mysql.mysql -R /var/lib/mysql/
[root@centos74-tmp mysql]# ll /var/lib/
total 4
……
drwxr-xr-x 2 mysql mysql 47 Jun 15 09:53 mysql
……
再次尝试重启MySQL,发现启动成功!!!
[root@centos74-tmp mysql]# service mysqld start
service mysqld start
Starting MySQL. SUCCESS!
[root@centos74-tmp mysql]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::4118 :::* LISTEN
tcp6 0 0 :::22
[root@centos74-tmp mysql]# vim /var/log/mariadb/mariadb.log
2019--15T01::.010780Z [Note] Binlog end
--15T01::.010938Z [Note] Shutting down plugin 'ngram'
--15T01::.010973Z [Note] Shutting down plugin 'ARCHIVE'
--15T01::.010984Z [Note] Shutting down plugin 'partition'
--15T01::.010992Z [Note] Shutting down plugin 'BLACKHOLE'
--15T01::.011002Z [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
--15T01::.011161Z [Note] Shutting down plugin 'CSV'
--15T01::.011175Z [Note] Shutting down plugin 'MEMORY'
--15T01::.011196Z [Note] Shutting down plugin 'MRG_MYISAM'
--15T01::.011216Z [Note] Shutting down plugin 'MyISAM'
--15T01::.011786Z [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
--15T01::.011817Z [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
--15T01::.011833Z [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
--15T01::.011838Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
--15T01::.011842Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
--15T01::.011846Z [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
--15T01::.011851Z [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
--15T01::.011855Z [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
--15T01::.011859Z [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
--15T01::.011864Z [Note] Shutting down plugin 'INNODB_SYS_TABLES'
--15T01::.011868Z [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
--15T01::.011872Z [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
--15T01::.011876Z [Note] Shutting down plugin 'INNODB_FT_CONFIG'
--15T01::.011880Z [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
--15T01::.011885Z [Note] Shutting down plugin 'INNODB_FT_DELETED'
--15T01::.011889Z [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
--15T01::.011893Z [Note] Shutting down plugin 'INNODB_METRICS'
--15T01::.011897Z [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
--15T01::.011901Z [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
--15T01::.011906Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
--15T01::.011910Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
--15T01::.011914Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
--15T01::.011919Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
--15T01::.011923Z [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
--15T01::.011927Z [Note] Shutting down plugin 'INNODB_CMPMEM'
--15T01::.011947Z [Note] Shutting down plugin 'INNODB_CMP_RESET'
--15T01::.011952Z [Note] Shutting down plugin 'INNODB_CMP'
--15T01::.011956Z [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
--15T01::.011961Z [Note] Shutting down plugin 'INNODB_LOCKS'
--15T01::.011965Z [Note] Shutting down plugin 'INNODB_TRX'
--15T01::.011969Z [Note] Shutting down plugin 'InnoDB'
--15T01::.012134Z [Note] InnoDB: FTS optimize thread exiting.
--15T01::.012281Z [Note] InnoDB: Starting shutdown...
--15T01::.112614Z [Note] InnoDB: Dumping buffer pool(s) to /usr/local/mysql/data/ib_buffer_pool
--15T01::.117111Z [Note] InnoDB: Buffer pool(s) dump completed at ::
--15T01::.836575Z [Note] InnoDB: Shutdown completed; log sequence number
--15T01::.839893Z [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
--15T01::.839924Z [Note] Shutting down plugin 'sha256_password'
--15T01::.839932Z [Note] Shutting down plugin 'mysql_native_password'
--15T01::.840120Z [Note] Shutting down plugin 'binlog'
--15T01::.840407Z [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
—— 以上仅为我个人遇到的情况,可能会有所不同,请根据实际情况,依据日志报错一个个处理。