[Linux]MySQL安装

0. 一些说明

MySQL版本:5.6.37
MySQL安装目录:/usr/lcoal/mysql

1. 下载MySQL的tar.gz版本,解压并复制到安装的目录下

1
2
tar -zxvf mysql-file.tar.gz
cp -R mysql-file /usr/local/mysql

2. 进入MySQL安装目录

1
cd /usr/local/mysql

3. 执行./scripts/mysql_install_db –help查看初始化安装参数

1
./scripts/mysql_install_db --help

正常的话出现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Usage: ./scripts/mysql_install_db [OPTIONS]
--basedir=path The path to the MySQL installation directory.
--builddir=path If using --srcdir with out-of-directory builds, you
will need to set this to the location of the build
directory where built files reside.
--cross-bootstrap For internal use. Used when building the MySQL system
tables on a different host than the target.
--datadir=path The path to the MySQL data directory.
If missing, the directory will be created, but its
parent directory must already exist and be writable.
--defaults-extra-file=name
Read this file after the global files are read.
--defaults-file=name Only read default options from the given file name.
--force Causes mysql_install_db to run even if DNS does not
work. In that case, grant table entries that
normally use hostnames will use IP addresses.
--help Display this help and exit.
--ldata=path The path to the MySQL data directory. Same as --datadir.
--no-defaults Don't read default options from any option file.
--keep-my-cnf Don't try to create my.cnf based on template.
Useful for systems with working, updated my.cnf.
Deprecated, will be removed in future version.
--random-passwords Create and set a random password for all root accounts
and set the "password expired" flag,
also remove the anonymous accounts.
--rpm For internal use. This option is used by RPM files
during the MySQL installation process.
--skip-name-resolve Use IP addresses rather than hostnames when creating
grant table entries. This option can be useful if
your DNS does not work.
--srcdir=path The path to the MySQL source directory. This option
uses the compiled binaries and support files within the
source tree, useful for if you don't want to install
MySQL yet and just want to create the system tables.
--user=user_name The login username to use for running mysqld. Files
and directories created by mysqld will be owned by this
user. You must be root to use this option. By default
mysqld runs using your current login name and files and
directories that it creates will be owned by you.
Any other options are passed to the mysqld program.

若出现以下情况:

1
2
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

则执行perl-Data-Dumper的安装

1
yum install perl-Data-Dumper

4. 创建MySQL组和用户

1
2
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql

5. 执行MySQL初始化安装命令

1
./scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql

若出现了[ERROR]了,尝试删除data目录再重新执行

【更新】:当安装MySQL 5.7.20的时候,发现mysql_install_db已经被废弃(deprecated)了,而且mysql_install_db执行文件存放位置也从scripts目录转移到了bin目录下;此外,由于该命令废弃了,所以当前建议使用mysqld –initialize,提示如下代码:

1
2
2018-01-18 17:31:30 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-01-18 17:31:30 [ERROR] The data directory needs to be specified.

mysqld存放在bin目录下,查看详细信息可用命令./mysqld --verbose --help,所以初始化安装命令变为:

1
./mysqld --initialize --basedir=/usr/local/mysql --user=mysql

6. my.cnf模版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[client]
default-character-set = utf8mb4

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data/
port = 3306
# server_id = .....
# socket = .....
# 忽略大小写
lower_case_table_names = 1
# 默认编码
character_set_server = utf8mb4
init_connect = 'SET NAMES utf8mb4'

log_error = /usr/local/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1 # 慢查询时间,超过1s则为慢查询
slow_query_log_file = /usr/local/mysql/log/mysql-slow.log

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysql]
default-character-set = utf8mb4

针对版本5.7.25

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[client]
default-character-set = utf8mb4
socket = /usr/local/mysql/data/mysql.sock

[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3360
# server_id = .....
socket = /usr/local/mysql/data/mysql.sock
pid_file = /usr/local/mysql/data/mysql.pid
# 忽略大小写
lower_case_table_names = 1
# 默认编码
character_set_server = utf8mb4
init_connect = 'SET NAMES utf8mb4'

log_error = /usr/local/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1 # 慢查询时间,超过1s则为慢查询
slow_query_log_file = /usr/local/mysql/data/mysql-slow.log

# 开启查询缓存
# 针对警告"TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option",版本5.7
explicit_defaults_for_timestamp=true

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO

my.cnf找寻顺序:
/etc/my.cnf:Global options
/etc/mysql/my.cnf:Global options
SYSCONFDIR/my.cnf:Global options
$MYSQL_HOME/my.cnf:Server-specific options (server only)
defaults-extra-file:The file specified with –defaults-extra-file, if any
~/.my.cnf:User-specific options
~/.mylogin.cnf:User-specific login path options (clients only)

7. 把文件夹权限设为mysql用户

1
chown -R mysql:mysql /usr/local/mysql/data

除了data文件夹,log日志文件也需要mysql用户权限,若log文件位置如my.cnf模版,则把该log文件夹权限也设为mysql用户

1
chown -R mysql:mysql /usr/local/mysql/log

8. 查看初始化安装命令结果,根据建议执行下一步操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h iZwz9aj3xjio5q3ujyc9ecZ password 'new-password'

Alternatively you can run:

/usr/local/mysql/bin/mysql_secure_installation

which 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 . ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

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

WARNING: Found existing config file /usr/local/mysql/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr/local/mysql/my-new.cnf,
please compare it with your file and take the changes you need.

9. 建议1: MySQL自启动

自启动有两个步骤需要做的,一是把MySQL添加到系统服务,二是添加到自启动列表

1
2
3
4
5
6
# 软链接方式添加到系统服务中
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 添加MySQL服务到自启动列表, 前提保证服务拥有‘x’权限
chkconfig --add mysqld
# 查看自启动服务是否包含MySQL服务
chkconfig --list

10. 建议2: 执行bin/mysql_secure_installation

1
./bin/mysql_secure_installation

这个命令会处理以下内容:

  • 设置root密码,当然也可以跳过
  • 是否删除匿名用户
  • 是否禁止root用户远程登录
  • 是否删除test数据库
  • 是否立即刷新已变更的权限和内容

【更新】5.7.20版本root用户会有个临时的密码,可通过my.cnf中的mysql-error.log查看到A temporary password is generated for root@localhost: xxx的语句,xxx就是临时的密码了。

若执行过程中出现以下错误:

1
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

则说明MySQL尚未启动,执行service mysqld start启动。

(•̀ᴗ•́)و ̑̑

Share