安装mysql

摘要:
MySQL是一个关系数据库管理系统,由瑞典MySQL AB开发,目前属于Oracle。然而,在步骤4和步骤5中重复输入可执行目录是很乏味的。如果您想在将来简化操作,可以执行以下操作。

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。

一、Window版本

1、下载

1 MySQL Community Server 5.7.16
2  
3 http://dev.mysql.com/downloads/mysql/

2、解压

如果想要让MySQL安装在指定目录,那么就将解压后的文件夹移动到指定目录,如:C:mysql-5.7.16-winx64

3、初始化

MySQL解压后的 bin 目录下有一大堆的可执行文件,执行如下命令初始化数据:

1 cd c:mysql-5.7.16-winx64in
2  
3 mysqld --initialize-insecure

4、启动MySQL服务

执行命令从而启动MySQL服务

1 #进入可执行文件目录
2 cd c:mysql-5.7.16-winx64in
3  
4 #启动MySQL服务
5 mysqld

5、启动MySQL客户端并连接MySQL服务

由于初始化时使用的【mysqld --initialize-insecure】命令,其默认未给root账户设置密码

1 #进入可执行文件目录
2 cd c:mysql-5.7.16-winx64in
3  
4 #连接MySQL服务器
5 mysql -u root -p
6  
7 #提示请输入密码,直接回车

输入回车,见下图表示安装成功:

安装mysql第1张

到此为止,MySQL服务端已经安装成功并且客户端已经可以连接上,以后再操作MySQL时,只需要重复上述4、5步骤即可。但是,在4、5步骤中重复的进入可执行文件目录比较繁琐,如想日后操作简便,可以做如下操作。

a. 添加环境变量

将MySQL可执行文件添加到环境变量中,从而执行执行命令即可

【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 -->【将MySQL的bin目录路径追加到变值值中,用 ; 分割】
如:
C:Program Files (x86)ParallelsParallels ToolsApplications;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;%SYSTEMROOT%System32WindowsPowerShellv1.0;C:Python27;C:Python35;C:mysql-5.7.16-winx64in

如此一来,以后再启动服务并连接时,仅需:

1 #启动MySQL服务,在终端输入
2 mysqld
3  
4 #连接MySQL服务,在终端输入:
5 mysql -u root -p

b. 将MySQL服务制作成windows服务

上一步解决了一些问题,但不够彻底,因为在执行【mysqd】启动MySQL服务器时,当前终端会被hang住,那么做一下设置即可解决此问题:

1 #制作MySQL的Windows服务,在终端执行此命令:
2 "c:mysql-5.7.16-winx64inmysqld" --install
3  
4 #移除MySQL的Windows服务,在终端执行此命令:
5 "c:mysql-5.7.16-winx64inmysqld" --remove

注册成服务之后,以后再启动和关闭MySQL服务时,仅需执行如下命令:

1 #启动MySQL服务
2 net start mysql
3  
4 #关闭MySQL服务
5 net stop mysql

二、Linux版本

1、采用二进制包安装mysql

二进制软件包名称

mysql-5.5.49-linux2.6-x8.6_64.tar.gz

添加用户和组

groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
tail -1 /etc/passwd
id mysql

开始安装MySQL

1 [root@template ]#mkdir -p /home/oldboy/tools
2 [root@template ]#cd /home/oldboy/tools
3 [root@template tools]#wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz
4 [root@template tools]#tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz 
5 [root@template tools]#mkdir -p /application/
6 [root@template tools]#mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
7 [root@template tools]#ln -s /application/mysql-5.5.49/ /application/mysql
8 [root@template tools]#ls -l /application/mysql
9 lrwxrwxrwx 1 root root 26 10月 27 10:28 /application/mysql -> /application/mysql-5.5.49/
10 
11 [root@template tools]#cd /application/mysql/
12 [root@template mysql]#ls -l support-files/*.cnf
13 -rw-r--r-- 1 7161 wheel  4691 3月   1 2016 support-files/my-huge.cnf
14 -rw-r--r-- 1 7161 wheel 19759 3月   1 2016 support-files/my-innodb-heavy-4G.cnf
15 -rw-r--r-- 1 7161 wheel  4665 3月   1 2016 support-files/my-large.cnf
16 -rw-r--r-- 1 7161 wheel  4676 3月   1 2016 support-files/my-medium.cnf
17 -rw-r--r-- 1 7161 wheel  2840 3月   1 2016 support-files/my-small.cnf
18 
19 #复制my.cnf 配置文件
20 [root@template mysql]#/bin/cp support-files/my-small.cnf /etc/my.cnf
21 [root@template mysql]#mkdir -p /application/mysql/data
22 [root@template mysql]#chown -R mysql.mysql /application/mysql/
View Code

初始化数据库

1 [root@template mysql]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
2 Installing MySQL system tables...
3 161027 10:30:22 [Note] /application/mysql/bin/mysqld (mysqld 5.5.49) starting as process 1958...
4 OK
5 Filling help tables...
6 161027 10:30:23 [Note] /application/mysql/bin/mysqld (mysqld 5.5.49) starting as process 1965...
7 OK
8 
9 To start mysqld at boot time you have to copy
10 support-files/mysql.server to the right place foryour system
11 
12 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
13 To do so, start the server, then issue the following commands:
14 
15 /application/mysql/bin/mysqladmin -u root password 'new-password'
16 /application/mysql/bin/mysqladmin -u root -h template.com password 'new-password'
17 
18 Alternatively you can run:
19 /application/mysql/bin/mysql_secure_installation
20 
21 which will also give you the option of removing the test
22 databases and anonymous user created by default.  This is
23 strongly recommended forproduction servers.
24 
25 See the manual formore instructions.
26 
27 You can start the MySQL daemon with:
28 cd /application/mysql ; /application/mysql/bin/mysqld_safe &
29 
30 You can test the MySQL daemon with mysql-test-run.pl
31 cd /application/mysql/mysql-test ; perl mysql-test-run.pl
32 
33 Please report any problems at http://bugs.mysql.com/
View Code

添加数据库文件

1 [root@template mysql]#cp support-files/mysql.server  /etc/init.d/mysqld
2 [root@template mysql]#chmod +x /etc/init.d/mysqld
3 [root@template mysql]#ll /etc/init.d/mysqld 
4 -rwxr-xr-x 1 root root 10880 Oct 27 10:31 /etc/init.d/mysqld

二进制默认路径为/usr/local/mysql 启动脚本里面的路径要更改

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

启动mysql数据库

1 [root@template mysql]#/etc/init.d/mysqld start
2 Starting MySQL.. SUCCESS! 

检查mysql数据库是否启动

1 [root@template mysql]#netstat -lntup|grep mysql
2 tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2224/mysqld       

设置mysql 开机自启动

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

配置开机自启动

1 echo "#mysql start by huzhihua at 2016-10-27" >>/etc/rc.local 
2 echo "/etc/init.d/mysqld start" >>/etc/rc.local 
3 
4 [root@template mysql]#tail -2 /etc/rc.local 
5 #mysql start by huzhihua at 2016-10-27
6 /etc/init.d/mysqld start

配置mysql命令的全局使用路径

1 [root@template mysql]#echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
2 [root@template mysql]#tail -1 /etc/profile
3 export PATH=/application/mysql/bin:$PATH
4 [root@template mysql]#source /etc/profile
5 [root@template mysql]#echo $PATH
6 /application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

登录mysql

别外三种登录方法:

1 mysql -uroot -p,
2 mysql -uroot
3 mysql -uroot -p 'oldboy123'
4 
5 [root@template mysql]#mysql
6 Welcome to the MySQL monitor.  Commands end with ; org.
7 Your MySQL connection id is 1
8 Server version: 5.5.49MySQL Community Server (GPL)
9 
10 Copyright (c) 2000, 2016, Oracle and/orits affiliates. All rights reserved.
11 
12 Oracle is a registered trademark of Oracle Corporation and/orits
13 affiliates. Other names may be trademarks of their respective
14 owners.
15 
16 Type 'help;' or 'h' for help. Type 'c'to clear the current input statement.
17 
18 mysql> exit   #退出

请退出mysql,再设置密码

mysqladmin -u root password '123456'

2、采用yum的方式安装mysql

安装:

1
yum install mysql-server  

服务端启动

1
mysql.server start

免责声明:文章转载自《安装mysql》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ClamAVNginx中文手冊下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables opt

mysql跳过权限: mysqld -nt --skip-grant-tables opt 登录:mysql -uroot -p 修改root密码 set password for 'root'@'localhost' = password('新密码'); 报错:ERROR 1290 (HY000): The MySQL server is running...

Flask-SQLAlchemy操作

Flask-SQLAlchemy SQLAlchemy 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架。该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据API执行SQL并获取执行结果。 pip3 install sqlalchemy 组成部分: Engine,框架...

PHP无法连接MYSQL8.0一般处理办法

以 mysql_native_password 加密方式修改密码 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码 FLUSH PRIVILEGES; #刷新权限 修改MYSQL.INI mysql8.0 默认用户加密...

Linux 服务器安全加固(等保 2.0 标准)

Linux 服务器安全加固(等保 2.0 标准) 2020年11月11日原创Linux 浏览 286评论 0 Linux 服务器安全加固(等保 2.0 标准) 身份鉴别 配置账户密码策略 参考配置操作: 1、 1234567 # vim /etc/login.defs修改下列参数为建议值PASS_MAX_DAYS 30 #密码有效期为30天...

C++中使用Json的方法

使用 C++ 处理 JSON 数据交换格式   一、摘要     JSON 的全称为:JavaScript Object Notation,顾名思义,JSON 是用于标记 Javascript 对象的,JSON官方的解释为:JSON 是一种轻量级的传输数据格式。 本文并不具体介绍 JSON 本身的细节,旨在讨论怎样使用 C++ 语言来处理 JSON。关于...

MySQL中多表删除方法

如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助。 1、从MySQL数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或DELETE...