打赏 jQuery火箭图标返回顶部代码

MySQL安装与配置介绍

MySQl介绍

官方站点:http://www.mysql.com/ 

MySQL是一个开放源码的小型关联式数据库管理系统。目前MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。

MySQl数据库分类

1)社区版

2)商业版

3)cluster集群

一.安装简介

1)创建mysql用户的账号

首先以root身份登录到Linux系统中,然后执行如下命令创建mysql组及用户账号:

1 [root@king ~]# groupadd mysql
2 [root@king ~]# useradd -s /sbin/nologin -g mysql -M mysql
useradd命令的参数简要说明
    -s/sbin/nologin表示禁止该用户登录,只需要角色存在即可,加强安全。
    -g mysql指定mysql用户属于mysql组。
    -M表示不创建用户家目录,因为没有需要。
    groupadd和useradd这两条命令也可以用useradd-s/sbin/nologin-M mysql替代。

然后检查刚刚创建的mysql用户和组,如下:

1 [root@king ~]# tail -1 /etc/passwd
2 mysql:x:501501::/home/mysql:/sbin/nologin
3 [root@king ~]# id mysql 
4 uid=501(mysql)gid=501(mysql)组=501(mysql)

现在建立一个存放所有安装软件的固定目录,如下:

1 [root@king ~]# mkdir -p /home/king/tools
2 [root@king ~]# cd /home/king/tools/

二.获取MySQL软件包

1.MySQL软件包的下载地址为: https://dev.mysql.com/ ,官网下载示例点我

2.现在以rz命令从本地安装来演示部署过程,执行时如提示无rz命令,可先执行yum install lrzsz-y安装,如下:

1 [root@king tools]# cd /home/oldboy/tools/
2 [root@king tools]# rz
3 rz waiting to receive.开始zmodem传输。按Ctrl+C取消正在传输mysql-5.5.32-linux2.6-x86_64.tar.gz...
4 100% 182346 KB 5525 KB/s 000011 0 错误
5 [root@king tools]# ls -sh mysql-5.5.32-linux2.6-x86_64.tar.gz 
6 179M mysql-5.5.32-linux2.6-x86_64.tar.gz
三 采用二进制方式安装MySQL
(1)解压并移动MySQL二进制软件包到指定的安装路径,命令如下:
1 [root@king tools]# tar xf mysql-5.7.32-linux2.6-x86_64.tar.gz
2 #<==解压
3 [root@king tools]# mkdir -p /application/ #<==创建安装目录,如果有就不用创建了
4 [root@king tools]# mv mysql-5.7.32-linux2.6-x86_64 /application/mysql-5.7.32 
5 #<==移动并改名目录
(2)创建软链接,生成去掉版本号的访问路径并查看,命令如下:
1 [root@king tools]# ln -s /application/mysql-5.7.32/ /application/mysql

三.初始化MySQL数据库文件

初始化命令如下:
 1 [root@king mysql]# mkdir -p /application/mysql/data
 2 #<==建立MySQL数据文件目录
 3 [root@king mysql]# chown -R mysql.mysql /application/mysql/
 4 #<==授权mysql用户管理MySQL的安装目录
 5 [root@king mysql]# /application/mysql/scripts/mysql_install_db --basedir=
 6 /application/mysql --datadir=/application/mysql/data --user=mysql
 7 #<==初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别
 8 的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
 9 WARNING: The host 'www' could not be looked up with resolveip.
10 This probably means that your libc libraries are not 100 % compatible
11 with this binary MySQL version. The MySQL daemon,mysqld,should work
12 normally with the exception that host name resolving will not work.
13 This means that you should use IP addresses instead of hostnames
14 when specifying MySQL privileges !
15 Installing MySQL system tables...
16 OK
17 Filling help tables...
18 OK
19 To start mysqld at boot time you have to copy
20 support-files/mysql.server to the right place for your system
21 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
22 To do so,start the server,then issue the following commands:
23 /application/mysql/bin/mysqladmin -u root password 'new-password'
24 /application/mysql/bin/mysqladmin -u root -h www password 'new-password'
25 Alternatively you can run:
26 /application/mysql/bin/mysql_secure_installation
27 which will also give you the option of removing the test
28 databases and anonymous user created by default.  This is
29 strongly recommended for production servers.
30 See the manual for more instructions.
31 You can start the MySQL daemon with:
32 cd /application/mysql;/application/mysql/bin/mysqld_safe &
33 You can test the MySQL daemon with mysql-test-run.pl
34 cd /application/mysql/mysql-test;perl mysql-test-run.pl
35 Please report any problems with the /application/mysql/scripts/mysqlbug script!

四.初始化故障排错集锦

错误示例1:WARNING:The host'mysql'could not be looked up with resolveip.
需修改主机名的解析,使其和uname-n一样,修改后的结果如下:
1 [root@king mysql]# grep `uname -n` /etc/hosts
2 127.0.0.1 www localhost.localdomain localhost
错误示例2:ERROR:1004 Can't create file'/tmp/#sql300e_1_0.frm'(errno:13)
原因是/tmp目录的权限有问题。
解决办法为处理/tmp目录,如下:
1 [root@king mysql]# ls -ld /tmp
2 drwxrwxrwt. 4 root root 4096 Aug 17 1144 /tmp
3 [root@king mysql]# chmod -R 4777 /tmp/

五.配置并启动MySQL

1)设置MySQL启动脚本,如下:

1 [root@king mysql]# cp support-files/mysql.server /etc/init.d/mysqld
2 #<==拷贝MySQL启动脚本到MySQL的命令路径
3 [root@king mysql]# chmod +x /etc/init.d/mysqld 
4 #<==使脚本可执行

2)MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里是/usr/local/mysql的路径都需要替换,如下:

1 [root@king mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

3)启动MySQL数据库,如下:

1 [root@king mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

4)检查MySQL数据库是否启动,如下:

1 [root@king mysql]# netstat -lntup|grep mysql
2 tcp 0 0 0.0.0.03306 0.0.0.0:* LISTEN 9446/mysqld

5)设置MySQL开机自起动,如下:

1 [root@king mysql]# chkconfig --add mysqld
2 [root@king mysql]# chkconfig mysqld on
3 [root@king mysql]# chkconfig --list mysqld
4  mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

6)配置mysql命令的全局使用路径,如下:

1 [root@king mysql]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
2 #<==注意,echo后是单引号,双引号是不行滴。
3 [root@king mysql]# tail -1 /etc/profile
4 export PATH=/application/mysql/bin:$PATH
5 [root@king mysql]# source /etc/profile
6 #<==执行source使上一行添加到/etc/profile中的内容直接生效。

7)登陆MySQL数据库,如下

1 [root@king mysql]# mysql  #<==直接敲就进入数据库了,而且身份还是root

六.MySQl安全配置

为MySQL的root用户设置密码,命令如下:

 1 [root@king mysql]# mysqladmin -u root password 'liang123' #<==更改默认密码。
 2 [root@king mysql]# mysql #<==无法直接登录了
 3 ERROR 104528000): Access denied for user 'root'@'localhost'using password: NO)
 4 [root@king mysql]# mysql -uroot -p #<==新的登录方式
 5 Enter password: #<==输入新密码liang123
 6 Welcome to the MySQL monitor. Commands end with;or \g.
 7 Your MySQL connection id is 4
 8 Server version: 5.7.32 MySQL Community Server(GPL)
 9 Copyright(c)20002018,Oracle and/or its affiliates. All rights reserved.
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14 mysql>
posted @ 2018-08-17 14:43  浪漫De刺猬  阅读(457)  评论(0编辑  收藏  举报