002、MySQL单实例二进制安装(mysql 5.6.35)

1、单实例安装过程中:
  • 创建MySQL用户
  • basedir
  • datadir
  • my.cnf
  • 授权
2、启动过程中
  • 初始化MySQL
  • 初始化数据的用户
  • 启动使用mysqld_safe后面第一个参数必须跟--defaults-file
  • 数据库用户清理,只保留root一个用户
  • root用户设置密码
  • 刷新权限表
  • 登录数据库假如密码是root,那么登陆的时候,不要写明密码,防止历史命令泄露密码,比如mysql -uroot -p登录,不要使用mysql -uroot -proot
  • 丢失密码如何处理
  • 编写自动化安装脚本

MySQL数据库安装

三部走、一步曲:
  1. 配置MySQL的家目录,一般放在/usr/local/mysql
  2. 配置MySQL的数据目录,一般在/data/mysql
  3. 配置MySQL的配置文件my.conf,一般在/etc/my.cnf
  4. MySQL单实例安装启动

一、配置MySQL的家目录

上传包,解压缩:
[root@localhost ~]# mkdir -p /opt/software/mysql
[root@localhost ~]# cd /opt/software/mysql
[root@localhost mysql]# gunzip mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
[root@localhost mysql]# tar -xvf mysql-5.6.35-linux-glibc2.5-x86_64.tar 
[root@localhost mysql]# ll
total 1100244
drwxr-xr-x 13 root root       4096 Jan 18 21:09 mysql-5.6.35-linux-glibc2.5-x86_64
-rw-r--r--  1 root root 1125529600 Jan 18 21:07 mysql-5.6.35-linux-glibc2.5-x86_64.tar
[root@localhost mysql]# mv mysql-5.6.35-linux-glibc2.5-x86_64 mysql
[root@localhost mysql]# ll
total 1100244
drwxr-xr-x 13 root root       4096 Jan 18 21:09 mysql
-rw-r--r--  1 root root 1125529600 Jan 18 21:07 mysql-5.6.35-linux-glibc2.5-x86_64.tar
[root@localhost mysql]# mv mysql /usr/local

二、配置MySQL的数据目录

[root@localhost ~]# mkdir -p /data/mysql
授权:
[root@localhost home]# useradd mysql
[root@localhost home]# chown mysql:mysql -R /usr/local/mysql
[root@localhost home]# chown mysql:mysql -R /data/mysql

三、配置MySQL的配置文件my.cnf

[client]
port	= 3306
socket	= /tmp/mysql.sock
#default-character-set=utf8

[mysql]
#default-character-set=utf8

[mysqld]
port	= 3306
socket	= /tmp/mysql.sock
basedir	= /usr/local/mysql
datadir	= /data/mysql
open_files_limit    = 3072
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 512
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
slow_query_log = 1
slow_query_log_file = /data/mysql/slow.log
log-error = /data/mysql/error.log
long_query_time = 0.05
server-id = 3
log-bin = /data/mysql/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 1024M
expire_logs_days = 7
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
character-set-server=utf8
default-storage-engine=InnoDB
binlog_format=row

#gtid_mode=on
#log_slave_updates=1
#enforce_gtid_consistency=1
wait_timeout=300

transaction_isolation = REPEATABLE-READ
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 1434M
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_locks_unsafe_for_binlog = 0
[mysqldump]
quick
max_allowed_packet = 32M

四、MySQL单实例安装启动

二进制安装的方法,MySQL的命令可以直接使用,到MySQL家目录下的bin目录,启动MySQL。
启动MySQL,可以使用mysqld启动,也可以使用mysqld_safe启动。建议用第二种。
mysqld_safemysqld的守护进程。
启动之前,可以查看启动帮助:
[root@localhost bin]# ./mysqld_safe --help
Usage: ./mysqld_safe [OPTIONS]
  --no-defaults              Don't read the system defaults file
  --defaults-file=FILE       Use the specified defaults file
  --defaults-extra-file=FILE Also use defaults from the specified file
  --ledir=DIRECTORY          Look for mysqld in the specified directory
  --open-files-limit=LIMIT   Limit the number of open files
  --core-file-size=LIMIT     Limit core files to the specified size
  --timezone=TZ              Set the system timezone
  --malloc-lib=LIB           Preload shared library LIB if available
  --mysqld=FILE              Use the specified file as mysqld
  --mysqld-version=VERSION   Use "mysqld-VERSION" as mysqld
  --nice=NICE                Set the scheduling priority of mysqld
  --plugin-dir=DIR           Plugins are under DIR or DIR/VERSION, if
                             VERSION is given
  --skip-kill-mysqld         Don't try to kill stray mysqld processes
  --syslog                   Log messages to syslog with 'logger'
  --skip-syslog              Log messages to error log (default)
  --syslog-tag=TAG           Pass -t "mysqld-TAG" to 'logger'

All other options are passed to the mysqld program.
初始化mysql:
[root@localhost ~]# cd /usr/local/mysql/scripts
[root@localhost scripts]# ./mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/u01/data/mysql --user=mysql
以下是几种错误示例。

1、启动失败第一次(未初始化)

启动MySQL(&表示后台执行):
[root@localhost bin]# ./mysqld_safe --defauts-file=/etc/my.cnf &
[1] 5840
[root@localhost bin]# 170118 21:44:00 mysqld_safe Logging to '/data/mysql/error.log'.
170118 21:44:00 mysqld_safe Starting mysqld daemon with databases from /data/mysql
170118 21:44:02 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended

[1]+  Done                    ./mysqld_safe --defauts-file=/etc/my.cnf
可以看到进程文件/data/mysql/localhost.localdomain.pid ended,查看MySQL进程,并没有发现:
[root@localhost bin]# ps -ef|grep mysql
root      1492  2887  0 21:49 pts/3    00:00:00 grep mysql

查看错误日志:
[root@localhost bin]# vi /data/mysql/error.log
2017-01-18 21:51:31 2202 [Note] Plugin 'FEDERATED' is disabled.
/usr/local/mysql/bin/mysqld: Table 'mysql.plugin' doesn't exist
2017-01-18 21:51:31 2202 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
2017-01-18 21:51:31 2b6b14044520 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
……(内容省略)
2017-01-18 21:51:31 2202 [Note] InnoDB: 5.6.35 started; log sequence number 1600628
2017-01-18 21:51:31 2202 [ERROR] /usr/local/mysql/bin/mysqld: unknown variable 'defauts-file=/etc/my.cnf'
……(内容省略)
解决方法:
启动之前,需要初始化MySQL,目的是为了创建mysql库,MySQL系统表
[root@localhost ~]# cd /usr/local/mysql/scripts
[root@ning scripts]# ./mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/u01/data/mysql --user=mysql
FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db:
Data::Dumper
初始化命令失败,需要安装依赖包:
[root@ning yum.repos.d]# yum install -y *Perl*
[root@ning yum.repos.d]# yum install -y *autoconf*
[root@localhost ~]# cd /usr/local/mysql/scripts
[root@localhost scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf
Installing MySQL system tables...2017-01-18 22:03:32 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-18 22:03:32 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-01-18 22:03:32 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.35-log) starting as process 2320 ...
OK

Filling help tables...2017-01-18 22:03:34 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-18 22:03:34 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-01-18 22:03:34 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.35-log) starting as process 2343 ...
OK

……(内容省略,出现上面的2个OK表示初始化成功。)
到数据目录查看,多了很多文件:
[root@localhost ~]# cd /data/mysql
[root@localhost mysql]# ls
error.log  ib_logfile0  mysql             mysql-bin.000002  performance_schema
ibdata1    ib_logfile1  mysql-bin.000001  mysql-bin.index   test
2、启动失败第二次(未指定初始化用户,导致文件权限不够)
启动MySQL:
[root@localhost bin]# ./mysqld_safe --defaults-file=/etc/my.cnf &
[1] 3709
[root@localhost bin]# 170118 22:12:40 mysqld_safe Logging to '/data/mysql/error.log'.
170118 22:12:41 mysqld_safe Starting mysqld daemon with databases from /data/mysql
170118 22:12:41 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended

[1]+  Done                    ./mysqld_safe --defaults-file=/etc/my.cnf
查看MySQL进程:
[root@localhost bin]# ps -ef|grep mysql
root      4361  2887  0 22:13 pts/3    00:00:00 grep mysql
查看错误日志:
[root@localhost bin]# vi /data/mysql/error.log
……(内容省略)

^G/usr/local/mysql/bin/mysqld: File '/data/mysql/mysql-bin.index' not found (Errcode: 13 - Permission denied)
2017-01-18 22:12:34 3706 [ERROR] Aborting

2017-01-18 22:12:34 3706 [Note] Binlog end
2017-01-18 22:12:34 3706 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

^G/usr/local/mysql/bin/mysqld: File '/data/mysql/mysql-bin.index' not found (Errcode: 13 - Permission denied)
2017-01-18 22:12:41 4356 [ERROR] Aborting

2017-01-18 22:12:41 4356 [Note] Binlog end
2017-01-18 22:12:41 4356 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
解决方法:
初始化MySQL时需要指定用户,否则目录文件权限不对,比如数据目录
[root@localhost mysql]# ll
total 1575712
-rw-rw---- 1 mysql mysql       8241 Jan 18 22:12 error.log
-rw-rw---- 1 mysql mysql 1073741824 Jan 18 22:03 ibdata1
-rw-rw---- 1 mysql mysql  268435456 Jan 18 22:03 ib_logfile0
-rw-rw---- 1 mysql mysql  268435456 Jan 18 21:43 ib_logfile1
drwx------ 2 root  root        4096 Jan 18 22:03 mysql
-rw-rw---- 1 root  root       65444 Jan 18 22:03 mysql-bin.000001
-rw-rw---- 1 root  root     1187002 Jan 18 22:03 mysql-bin.000002
-rw-rw---- 1 root  root          58 Jan 18 22:03 mysql-bin.index
drwx------ 2 root  root        4096 Jan 18 22:03 performance_schema
drwx------ 2 root  root        4096 Jan 18 22:03 test
重新授权:
[root@localhost scripts]# chown mysql:mysql -R /data/mysql

3、启动失败第三次(mysqld_safe命令后面参数不对)

启动MySQL:
[root@localhost bin]# ./mysqld_safe --user=mysql --defaults-file=/etc/my.cnf
170118 22:32:25 mysqld_safe Logging to '/data/mysql/error.log'.
170118 22:32:25 mysqld_safe Starting mysqld daemon with databases from /data/mysql
170118 22:32:28 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
查看进程:
[root@localhost bin]# ps -ef|grep mysql
root      5183  2887  0 22:33 pts/3    00:00:00 grep mysql
查看日志:
……(内容省略)

2017-01-18 22:32:26 5158 [ERROR] /usr/local/mysql/bin/mysqld: unknown variable 'defaults-file=/et/my.cnf'
2017-01-18 22:32:26 5158 [ERROR] Aborting

……(内容省略)
解决方法(mysqld_safe后第一个参数必须跟参数文件)
[root@localhost bin]# ./mysqld_safe --defaults-file=/etc/my.cnf --user=mysql
170118 22:41:53 mysqld_safe Logging to '/data/mysql/error.log'.
170118 22:41:53 mysqld_safe Starting mysqld daemon with databases from /data/mysql
查看进程,可以看到父进程和子进程:
[root@localhost ~]# ps -ef|grep mysql
root      5344  2887  0 22:41 pts/3    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my.cnf --user=mysql
mysql     6003  5344  0 22:41 pts/3    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/error.log --open-files-limit=3072 --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root      6035  5309  0 22:44 pts/4    00:00:00 grep mysql
查看进程文件:
[mysql@localhost bin]$ cat /data/mysql/localhost.localdomain.pid
6003
查看端口:
root@localhost ~]# netstat -anlp|grep mysql
tcp        0      0 :::3306                     :::*                        LISTEN      6003/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     12867  6003/mysqld         /tmp/mysql.sock

五、配置MySQL环境变量

安装完成之后,需要配置环境变量,在/etc/profile或者mysql家目录的.bash_profile:
#**********************************************mysql***********************************************
export PATH=$PATH:/usr/local/mysql/bin
#**********************************************mysql***********************************************
如果在/etc/profile配置,使环境变量生效,其他用户也可以使用该环境变量。
使环境变量生效:
[root@localhost ~]# export PATH=$PATH:/usr/local/mysql/bin

六、MySQL用户名和密码的设置

  1. 使用root用户执行mysql命令行,查看数据库
  2. 进入mysql数据库,查看user表用户信息
  3. 删除无用用户,更新root用户密码
  4. 刷新权限表

1、使用root用户执行mysql命令行,查看数据库

MySQL数据库安装完成之后,查看有几个数据库:
[root@localhost ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@ning ~]# mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

2、进入MySQL库,查看有哪些表:

[root@ning ~]# 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 -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |

……内容省略

| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)
用户信息存储在user表中,查看user表结构信息,查询用户信息:
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(16)                          | NO   | PRI |                       |       |
| Password               | char(41)                          | NO   |     |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N                     |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| File_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| References_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N                     |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N                     |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N                     |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |                       |       |
| ssl_cipher             | blob                              | NO   |     | NULL                  |       |
| x509_issuer            | blob                              | NO   |     | NULL                  |       |
| x509_subject           | blob                              | NO   |     | NULL                  |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0                     |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0                     |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0                     |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0                     |       |
| plugin                 | char(64)                          | YES  |     | mysql_native_password |       |
| authentication_string  | text                              | YES  |     | NULL                  |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)

3、查询用户信息,删除无用用户,更新root密码:

mysql> select host,user,password from user;
+-----------------------+------+----------+
| host                  | user | password |
+-----------------------+------+----------+
| localhost             | root |          |
| localhost.localdomain | root |          |
| 127.0.0.1             | root |          |
| ::1                   | root |          |
| localhost             |      |          |
| localhost.localdomain |      |          |
+-----------------------+------+----------+
6 rows in set (0.00 sec)
整理清除一些无用用户,只保留root和localhost。
root用户是数据库超级管理员用户,为安全考虑,远程权限不能开放。
mysql> delete from user where host != 'localhost' or user != 'root';
Query OK, 5 rows affected (0.00 sec)

mysql> select host,user,password from user;
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | root |          |
+-----------+------+----------+
1 row in set (0.00 sec)
给root用户创建密码:
mysql> update user set password=password('root') where user='root';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
通过tcp/ip连接MySQL实例时,MySQL会先检查一张权限视图,用来判断发起请求的客户端ip是否允许连接到MySQL实例,该视图就是MySQL库的user表。
创建用户,限定网段,刷新权限表:
mysql> grant all privileges on *.* to 'zs'@'192.168.100.%' identified by '123456';
Query OK, 0 rows affected (0.07 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'root';

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
4、刷新权限表:
创建用户和密码之后,需要刷新权限表,目的是为了登录数据库的时候需要输入密码。
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql> exit
Bye
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
登录数据库输入密码:
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
关闭数据库的步骤:
[root@localhost ~]# mysqladmin -uroot -p shutdown
Enter password: 
170214 16:54:12 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
[1]+  Done                    mysqld_safe --defaults-file=/etc/my.cnf

七、root密码丢失遗忘的问题如何处理

1、root密码丢失,不能通过正常途径关闭数据库,强制关闭数据库:
[root@localhost ~]# ps -ef|grep mysql|grep -v grep|awk '{print $2}'|xargs kill -9
2、想要登陆数据库,没有密码的情况下需要跳过权限表:
[root@localhost ~]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables
[1] 3684
[root@localhost ~]# 170214 17:04:00 mysqld_safe Logging to '/data/mysql/localhost.localdomain.err'.
170214 17:04:00 mysqld_safe Starting mysqld daemon with databases from /data/mysql
3、重新设置密码
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 -A

Database changed
mysql> update user set password=password('root') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4、重新启动数据库:
[root@localhost ~]# mysqladmin -uroot -p shutdown
Enter password: 
170214 17:07:29 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
[1]+  Done                    mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables
[root@localhost ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[1] 3848
[root@localhost ~]# 170214 17:07:53 mysqld_safe Logging to '/data/mysql/localhost.localdomain.err'.
170214 17:07:53 mysqld_safe Starting mysqld daemon with databases from /data/mysql
5、查看MySQL详细错误根据错误号,例如:
[root@localhost bin]# perror 13
OS error code  13:  Permission denied
6、设置mysql开机启动:
编辑文件,并添加
vi /etc/rc.local
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
若系统是CentOS 7以上的版本,则还需要给rc.local文件执行权限:
[root@ning ~]# chmod +x /etc/rc.d/rc.local
[root@ning ~]# ls -l /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 534 Mar 16 21:21 /etc/rc.d/rc.local

八、总结

1、单实例安装过程中:
  • 创建MySQL用户
  • basedir
  • datadir
  • my.cnf
  • 授权
2、启动过程中
  • 初始化数据
  • 初始化数据的用户
  • 启动使用mysqld_safe后面第一个参数必须跟--defaults-file
  • 数据库用户清理,只保留root一个用户
  • root用户设置密码
  • 刷新权限表
  • 登录数据库假如密码是root,那么登陆的时候,不要写明密码,防止历史命令泄露密码,比如mysql -uroot -p登录,不要使用mysql -uroot -proot
  • 丢失密码如何处理
  • 编写自动化安装脚本




posted @ 2021-06-02 14:54  有点菜大人  阅读(106)  评论(0)    收藏  举报