Linux 系统安装mysql过程记录
-
资源清单
1 | mysql | mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz |
2 | linux | CentOS release 6.9 (Final) |
3 | gcc | gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) |
-
安装包
mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
安装包中有*glibc*的表示为源码编译后的二进制安装包,直接安装不需要再编译。
登陆官网下载:
-
安装步骤
-
创建用户
以root用户登录linux系统,
# useradd mysql
# passwd mysql
安装
用root用户上传mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz 到 /usr/local/
解压
# tar -zxvf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
修改文件名及所有者
# mv mysql-5.6.37-linux-glibc2.12-x86_64/ mysql-5.6.37
# chown mysql:mysql mysql-5.6.37/
切换mysql用户,进入mysql-5.6.37目录
执行安装命令
$./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.6.37 --datadir=/usr/local/mysql-5.6.37/data
执行安装命令如果报错:
FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db:
Data::Dumper
需要安装autoconf库
可以如下安装方式
离线安装
下载autoconf-2.65.tar.gz
&& tar -xzvf autoconf-2.65.tar.gz
&& cd autoconf-2.65
&& ./configure --prefix=/usr/local
make && make install
在线安装
yum -y install autoconf
配置文件
安装完成后进行配置操作
以root用户进入/usr/local/mysql-5.6.37/support-files/ 路径
执行
# cp my-default.cnf /etc/my.cnf
# cp mysql.server /etc/init.d/mysql
修改文件/etc/init.d/mysql,修改内容如下:
basedir=/usr/local/mysql-5.6.37
datadir=/usr/local/mysql-5.6.37/data
保存退出
修改/etc/init.d/mysql文件所有者
# chown mysql:mysql mysql
配置环境变量
#vim /etc/profile
在打开的文件中加入如下内容
export MY_SQL_HOME=/usr/local/mysql-5.6.37
export PATH=$PATH:$MY_SQL_HOME/bin
保存退出执行如下命令,使/etc/profile文件生效
#source /etc/profile
启动mysql
file | path | desc |
---|---|---|
mysqld | $mysql_home/bin | The MySQL Server,mysql的服务端 |
mysqld_safe | $mysql_home/bin | MySQL Server Startup Script,mysql服务端的启动脚本 |
mysql.server | $mysql_home/support-files | MySQL Server Startup Script,mysql服务端的启动脚本 |
mysql_multi | $mysql_home/bin | Manage Multiple MySQL Servers ,多个mysql服务端的管理器 |
mysqld:
SQL后台程序(即MySQL服务器)。要想使用客户端程序,该程序必须运行,因为客户端通过连接服务器来访问数据库。
通过mysqld启动mysql server,启动时指定启动参数,如:
$ mysqld --basedir=/usr/local/mysql-5.6.37 --datadir=/usr/local/mysql-5.6.37/data --character_set_server=utf8
mysqld可跟的参数可以用 $mysqld --help 查看,具体不细讲
如果每次启动都是相同的参数,可以把参数写入配置文件,配置文件位于/etc/my.cnf,如果没有,复制$mysql_home/my.cnf 到/etc路径下
(ps:windows版的配置文件my.ini,linux版的配置文件my.cnf)
该文件默认如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[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 = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# 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
mysqld从文件my.cnf中的 [mysqld] 和 [server] 下读取参数
my.cnf文件中默认只有 [mysqld] ,[server] 可以添加
如:basedir,datadir,字符集的参数没有,可以添加character_set_server=utf8,添加校对规则collation_server=utf8_bin(server,database存储数据区分大小写)
basedir=/usr/local/mysql-5.6.37
datadir=/usr/local/mysql-5.6.37/data
character_set_server=utf8
collation_server=utf8_bin
配置好/etc/my.cnf文件,可以直接使用mysqld启动mysql server
$ mysqld
mysqld_safe:
该文件是mysql server的启动脚本,mysqld_safe脚本从/etc/my.cnf文件的[mysqld]、 [server]和[mysqld_safe]部分读取参数。
如果my.cnf文件中均为默认,则会使用linux配置的mysql环境变量中找mysql安装路径,并使用该安装路径作为basedir/datadir参数的值启动mysql server
启动:
[mysql@localhost ~]$ mysqld_safe
170804 09:38:13 mysqld_safe Logging to '/usr/local/mysql-5.6.37/data/localhost.localdomain.err'.
170804 09:38:13 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql-5.6.37/data
mysql.server:
在Unix中的MySQL分发版中有mysql.server脚本。它可以用于使用System V-style运行目录来启动和停止系统服务的系统,例如Linux和Solaris。
mysql.server位于$mysql_home/support-files目录中。
复制该文件到/etc/init.d/路径下,文件名改为mysql,该文件中设置如下参数:
basedir=/usr/local/mysql-5.6.37
datadir=/usr/local/mysql-5.6.37/data
如果你使用Linux 服务器RPM软件包(MySQL-server-VERSION.rpm), mysql.server脚本将安装到/etc/init.d目录下,名为mysql。你不需要 手动安装。
启动:
[mysql@localhost ~]$ service mysql start
Starting MySQL SUCCESS!
停止:
[mysql@localhost ~]$ service mysql stop
Shutting down MySQL.. SUCCESS!
[mysql@localhost ~]$
以这种方式启动,启动时会读取/etc/my.cnf配置文件,从[mysqld]、 [mysql.server]部分读取参数
修改root用户密码
$ service mysql start
登录mysql及改密码与配置远程访问
修改root用户密码
$ mysqladmin -u root password xxxx
xxxx为自定义的root用户密码