LAMP环境搭建 一 安装MySQL

LAMP是Linux Apache MySQL PHP的简写,即把Apache、MySQL以及PHP安装在Linux系统上,组成一个环境来运行PHP的脚本语言,通常是网站。

Apache是最常用的web服务软件,而MySQL是比较小型的数据库软件,这两个软件以及PHP都可以安装在一台机器上,也可以分开安装,但Apache和PHP必须安装在同一台机器上,因为PHP是作为Apache的一个模块存在的。

安装MySQL

MySQL官网:https://www.mysql.com/

MySQL是一个关系型数据库,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MariaDB官网:https://mariadb.com

MariaDB是MySQL的一个分支,由MySQL原作者开发,MariaDB 5.5版本对应MySQL 5.5版本,而MariaDB 10.0版本对应MySQL 5.6版本,有以下版本:

Community 社区版
Enterprise 企业版
GA(Generally Available)通用版
DMR(Development Milestone Release)开发里程碑发布版
RC(Release Candidate)发行候选版
Alpha 内部测试版
Beta 开放测试版
MySQL的最新版本是 5.7GA和8.0DMR ,MariaDB的最新版本是10.3.7 。

下载软件包

我们可以到官网去下载对应版本的MySQL/MariaDB的包,在这里,我使用的是免编译的二进制包。在CentOS7之前的版本都有区分32位和64位,CentOS7可以直接选择64位的包下载,带有x86_64字样的就是64位的包,带有i686字样的就是32位的包。

1.查看linux是多少位的:

# uname -i
x86_64

2.下载MySQL5.6的二进制包:


```powershell
# cd /usr/local/src/

# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
正在解析主机 mirrors.sohu.com (mirrors.sohu.com)... 221.236.12.140
正在连接 mirrors.sohu.com (mirrors.sohu.com)|221.236.12.140|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:316320366 (302M) [application/octet-stream]
正在保存至: “mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz”

100%[=========================================================================>] 316,320,366  475KB/s 用时 14m 48s

2018-06-26 14:47:30 (348 KB/s) - 已保存 “mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz” [316320366/316320366])

初始化

解压下载的包:

# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 

移动解压完的目录并更改名字为mysql(容易出现的错误,local下已经存在mysql文件,如果这样移动,会变成移动到mysql下,这样就会报错,mv前提前进入local看下是否已存在mysql文件夹)。

# [ -d /usr/local/mysql ] && mv /usrlocal/mysql /usr/local/mysql_old

# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

上面用到特殊符号&&,它表示当前面的命令执行成功时,才会执行后面的命令。如果/usr/local/mysql目录已经存在,就要把它改个名字,否则后面步骤会出错。

建立MySQL用户,启动mysql需要该用户:

# useradd -s /sbin/nologin mysql

# cd /usr/local/mysql

创建datadir,数据库文件会放到这里:

# mkdir -p data/mysql

更改权限,否则后面会报错:

# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 

移动解压完的目录并更改名字为mysql(容易出现的错误,local下已经存在mysql文件,如果这样移动,会变成移动到mysql下,这样就会报错,mv前提前进入local看下是否已存在mysql文件夹)。

# [ -d /usr/local/mysql ] && mv /usrlocal/mysql /usr/local/mysql_old

# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
```powershell
# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 

移动解压完的目录并更改名字为mysql(容易出现的错误,local下已经存在mysql文件,如果这样移动,会变成移动到mysql下,这样就会报错,mv前提前进入local看下是否已存在mysql文件夹)。

# [ -d /usr/local/mysql ] && mv /usrlocal/mysql /usr/local/mysql_old

# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
```powershell
# chown -R mysql:mysql data/mysql

进行初始化,指定用户为mysql,指定数据存放路径/usr/local/mysql/data/mysql:

# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql

Installing MySQL system tables...2018-06-26 15:59:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-26 15:59:53 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-06-26 15:59:53 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 3349 ...
2018-06-26 15:59:53 3349 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-06-26 15:59:53 3349 [Note] InnoDB: The InnoDB memory heap is disabled
2018-06-26 15:59:53 3349 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-06-26 15:59:53 3349 [Note] InnoDB: Memory barrier is not used
2018-06-26 15:59:53 3349 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-06-26 15:59:53 3349 [Note] InnoDB: Using Linux native AIO
2018-06-26 15:59:53 3349 [Note] InnoDB: Using CPU crc32 instructions
2018-06-26 15:59:53 3349 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-06-26 15:59:53 3349 [Note] InnoDB: Completed initialization of buffer pool
2018-06-26 15:59:53 3349 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2018-06-26 15:59:53 3349 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2018-06-26 15:59:53 3349 [Note] InnoDB: Database physically writes the file full: wait...
2018-06-26 15:59:53 3349 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2018-06-26 15:59:54 3349 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2018-06-26 15:59:55 3349 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2018-06-26 15:59:55 3349 [Warning] InnoDB: New log files created, LSN=45781
2018-06-26 15:59:55 3349 [Note] InnoDB: Doublewrite buffer not found: creating new
2018-06-26 15:59:55 3349 [Note] InnoDB: Doublewrite buffer created
2018-06-26 15:59:55 3349 [Note] InnoDB: 128 rollback segment(s) are active.
2018-06-26 15:59:55 3349 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-06-26 15:59:55 3349 [Note] InnoDB: Foreign key constraint system tables created
2018-06-26 15:59:55 3349 [Note] InnoDB: Creating tablespace and datafile system tables.
2018-06-26 15:59:55 3349 [Note] InnoDB: Tablespace and datafile system tables created.
2018-06-26 15:59:55 3349 [Note] InnoDB: Waiting for purge to start
2018-06-26 15:59:55 3349 [Note] InnoDB: 5.6.36 started; log sequence number 0
2018-06-26 15:59:55 3349 [Note] Binlog end
2018-06-26 15:59:55 3349 [Note] InnoDB: FTS optimize thread exiting.
2018-06-26 15:59:55 3349 [Note] InnoDB: Starting shutdown...
2018-06-26 15:59:57 3349 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2018-06-26 15:59:57 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-26 15:59:57 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-06-26 15:59:57 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 3371 ...
2018-06-26 15:59:57 3371 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-06-26 15:59:57 3371 [Note] InnoDB: The InnoDB memory heap is disabled
2018-06-26 15:59:57 3371 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-06-26 15:59:57 3371 [Note] InnoDB: Memory barrier is not used
2018-06-26 15:59:57 3371 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-06-26 15:59:57 3371 [Note] InnoDB: Using Linux native AIO
2018-06-26 15:59:57 3371 [Note] InnoDB: Using CPU crc32 instructions
2018-06-26 15:59:57 3371 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-06-26 15:59:57 3371 [Note] InnoDB: Completed initialization of buffer pool
2018-06-26 15:59:57 3371 [Note] InnoDB: Highest supported file format is Barracuda.
2018-06-26 15:59:57 3371 [Note] InnoDB: 128 rollback segment(s) are active.
2018-06-26 15:59:57 3371 [Note] InnoDB: Waiting for purge to start
2018-06-26 15:59:57 3371 [Note] InnoDB: 5.6.36 started; log sequence number 1625977
2018-06-26 15:59:57 3371 [Note] Binlog end
2018-06-26 15:59:57 3371 [Note] InnoDB: FTS optimize thread exiting.
2018-06-26 15:59:57 3371 [Note] InnoDB: Starting shutdown...
2018-06-26 15:59:59 3371 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

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:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:

  ./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 . ; ./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 ./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 ./my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
# echo $?
0

可以看到,上面出现了两个OK,这就说明初始化成功,还可以用echo $检验一下

配置MySQL

首先复制配置文件:

# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y

打开配置文件,做以下修改:

[client]
#客户端设置,即客户端默认的连接参数
port = 3306
#默认连接端口
socket = /usr/local/mysql/data/mysql.sock
#用于本地连接的socket套接字
default-character-set = utf8mb4
#编码
 
[mysqld] 
#服务端基本设置
port = 3306
#MySQL监听端口
socket = /usr/local/mysql/data/mysql.sock
#为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
pid-file = /usr/local/mysql/data/mysql.pid
#pid文件所在目录
basedir = /usr/local/mysql
#使用该目录作为根目录(安装目录)
datadir = /usr/local/mysql/data
tmpdir = /usr/local/mysql/data/tmp
#MySQL存放临时文件的目录
character_set_server = utf8mb4
#服务端默认编码(数据库级别)
collation_server = utf8mb4_bin
#服务端默认的比对规则,排序规则
user = root
#MySQL启动用户。如果是root用户就配置root,mysql用户就配置mysql
log-error=/usr/local/mysql/data/error.log
#错误日志配置文件(configure file)
secure-file-priv = null
#设置bin_log日志格式
binlog_format = mixed
#设置日志路径,注意路经需要mysql用户有权限写
log-bin = /usr/local/mysql//data/bin_logs/mysql-bin.log
#设置binlog清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#大binlog缓存大小
max_binlog_cache_size = 512m

然后复制启动脚本文件并修改其属性:

# cp support-files/mysql.server /etc/init.d/mysqld

# chmod 755 /etc/init.d/mysqld

然后修改启动脚本:

# vim /etc/init.d/mysqld
  
basedir=/usr/local/mysql  
datadir=/usr/local/mysql/data/mysql  

接下来将启动脚本加入系统服务项并设置开机启动:

# chkconfig --add mysqld

# chkconfig mysqld on

最后启动服务:

# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/mysql/localhost.localdomain.err'.
. SUCCESS!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值