LNMP系统———MySQL搭建
MySQL是一款深受广大用户喜欢的开源关系型数据库(马屁,但是事实),由瑞典MySQL AB 公司开发。2006年,MySQL AB被Sun公司收购。2008年,Sun被Oracle公司收购(Oracle数据库不开源哦),目前MySQL仍是一款开源数据库
MySQL如此受欢迎必有其特点:
性能卓越,服务稳定,体积小,安装使用简单,易于维护,使用成本低
开源,无版权制约,
历史悠久,社区活跃,遇到问题能快速得到帮助
支持多种操作系统,提供多种API,支持多种开发语言,尤其是对PHP天秀般的支持
。。。。。。。
安装方式简介
1、通过yum/rpm 包的方式安装,特点是简单,快速(制约因素只有网络),但是不能定制安装
2、二进制安装,直接解压软件包,简单配置即可使用,不用安装,更快速(下面介绍的就是这种方式)。
3、源码编译安装,特点是可以定制安装,但是需要指定参数编译,耗时较长
4、源码编译+yum/rpm安装。把源码制作层符合自己要求的rpm,放到yum仓库,通过yum安装,结合了上面1、3 的优点,既定制了参数,又能快速安装,但是安装人员(定制rpm包的人员)能力要求颇高(笔者努力中)
这里提供两个获取安装包的地址
https://dev.mysql.com/downloads/cluster/7.3.html
一、二进制安装
1、解压
[[email protected] tools]# tar -xf mysql-5.5.49-linux2.6-x86_64.tar.gz
[[email protected] tools]# mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
2、建立软连接,方便管理和使用
[[email protected] tools]# ln -s /application/mysql-5.5.49/ /application/mysql
[[email protected] tools]# ls /application/
mysql mysql-5.5.49 nginx nginx-1.6.3
3、创建用户
-s /sbin/nologin 指定这个用户不能登陆
-M 不创建该用户家目录
[[email protected] tools]# useradd mysql -s /sbin/nologin -M
[[email protected] tools]# id mysql
uid=508(mysql) gid=508(mysql) 组=508(mysql)
修改mysql安装目录的所有者和组
[[email protected] application]# chown -R mysql.mysql /application/mysql/
4、初始化数据库文件
--basedir=/application/mysql/ 数据库的安装位置
--datadir=/application/mysql/data/ 指定数据库文件的安装位置
--user=mysql 指定该数据库文件的用户
[[email protected] application]# cd /application/mysql
[[email protected] mysql]# ./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
Installing MySQL system tables...
190424 12:18:57 [Note] /application/mysql//bin/mysqld (mysqld 5.5.49) starting as process 12711 ...
OK
Filling help tables...
190424 12:18:57 [Note] /application/mysql//bin/mysqld (mysqld 5.5.49) starting as process 12718 ...
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:
/application/mysql//bin/mysqladmin -u root password 'new-password'
/application/mysql//bin/mysqladmin -u root -h web01 password 'new-password'
Alternatively you can run:
/application/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 /application/mysql/ ; /application/mysql//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql//mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
这里看到前面的两个 OK 则安装完成
可以用 tree /application/mysql/data/ 查看该目录结构
5、修改启动文件里的相关目录
[[email protected] bin]# cd /application/mysql/bin/
[[email protected] bin]# less mysqld_safe
[[email protected] bin]# cp mysqld_safe{,.ori}
修改启动文件,默认是/usr/local/,这里我安装在/application/,所以要替换成我的安装目录/application/
[[email protected] bin]# sed -i 's#/usr/local/#/application/#g' mysqld_safe
6、配置文件及初始化
把配置文件复制到系统配置文件目录/etc/下,同时改名为my.cnf
[[email protected] mysql]# /bin/cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
初始化:
& 作用是在后台执行MySQL服务,命令执行结束,还需按回车键
[[email protected] bin]# /application/mysql/bin/mysqld_safe --user=mysql &
[1] 12820
[[email protected] bin]# 190424 12:44:16 mysqld_safe Logging to '/var/log/mysqld.log'.
190424 12:44:16 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
190424 12:44:17 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
[1]+ Done /application/mysql/bin/mysqld_safe --user=mysql
查看服务端口 3306
[[email protected] mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 13239 mysql 10u IPv4 55975 0t0 TCP *:mysql (LISTEN)
进入MySQL
第一次是root登陆 无密码
[[email protected] mysql]# mysql
-bash: mysql: command not found
[[email protected] mysql]# /application/mysql/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.49 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.
mysql>
8、添加命令路径
二进制安装方式,MySQL的命令都在其安装目录的bin目录下,如笔者的/application/mysql/bin/目录,命令路径环境变量没有这个目录,所以要手动添加
[[email protected] mysql]# PATH="/application/mysql/bin/:$PATH"
[[email protected] mysql]# vim /etc/profile
##末尾添加一下内容
export PATH="/application/mysql/bin/:$PATH"
##执行配置文件使其生效,注意 . 后有个空格
[[email protected] mysql]# . /etc/profile
[[email protected] mysql]# echo $PATH
/application/mysql/bin/:/application/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
9、配置启动脚本
源码包提供有一个启动脚本support-files/mysql.server,把里面的路径改为自己的安装路径,复制到/etc/init.d/目录下,并改名为mysqld,最后加入开机自启动
[[email protected] mysql]# cp support-files/mysql.server{,.ori}
[[email protected] mysql]# sed -i 's#/usr/local/#/application/#g' support-files/mysql.server
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/init.d/mysqld
[[email protected] mysql]# /etc/init.d/mysqld stop
Shutting down MySQL...190424 13:20:31 mysqld_safe mysqld from pid file /application/mysql/data/web01.pid ended
SUCCESS!
[1]- Done /application/mysql/bin/mysqld_safe --user=mysql
[[email protected] mysql]# lsof -i :3306
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[[email protected] mysql]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 13599 mysql 10u IPv4 56724 0t0 TCP *:mysql (LISTEN)
[[email protected] mysql]# chkconfig mysqld on
[[email protected] mysql]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
10、配置密码
mysql也有一个root用户,前面提到首次root登陆是不需要密码的,此处就进行密码修改
[[email protected] mysql]# mysqladmin -u root password 'ysm123'
密码登陆 -u是指定登陆用户,-p是指定等录的密码,可以看到在 -u 和其指定的用户名之间的空格可以省略,-p也是
[[email protected] mysql]# mysql -uroot -p
Enter password: #此处输入密码
也可指定密码登陆
[[email protected] mysql]# mysql -uroot –pysm123
已有密码的用户修改密码,需要-p指定原密码
[[email protected] mysql]# mysqladmin -uroot -pysm123 password 123456
mysql 一键安装脚本
准备二进制安装包mysql-5.5.49-linux2.6-x86_64.tar.gz 把以下脚本和安装包放到同一个目录,然后执行脚本
注意,这个脚本只是按照步骤把命令执行一遍,没有加入开机自启动,也没有修改密码
[[email protected] tools]# vim installmysql.sh
mkdir -p /application/
tar -xf mysql-5.5.49-linux2.6-x86_64.tar.gz
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
ln -s /application/mysql-5.5.49/ /application/mysql
useradd mysql -s /sbin/nologin -M
chown -R mysql.mysql /application/mysql/
cd /application/mysql
./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
/bin/cp support-files/my-small.cnf /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/bin/cp /application/mysql/bin/mysqld_safe{,.ori}
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
/bin/cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
echo "/etc/init.d/mysqld start" >>/etc/rc.local
echo "##################" >>/etc/profile
echo "export PATH=\"/application/mysql/bin/:\$PATH\"" >>/etc/profile
. /etc/profile
/etc/init.d/mysqld start
mysql
下一篇是关于编译安装PHP,同时整合nginx、PHP和MySQL
后面会有一偏关于MySQL多实例和主从复制的文章对MySQL进行探讨,写好了再放链接