数据库MySQL——安装

By | 2019年12月3日
目录
[隐藏]

MySQL 安装

Mysql安装:

1、通过二进制的方式安装
二进制安装方式中,包括rpm版本以及glibc版本。
rpm版本就是在特定linux版本下编译的,如果你的linux版本匹配,就可以安装;
glibc版本是基于特定的glibc版本编译的;
glibc是GNU发布的libc库,即c运行库。glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc。
优点:安装和维护都比较方便,不需要编译。
缺点:可定制性差,可移植性差,不灵活

2、通过源代码编译的安装(mysql-xx.tar.gz)
优点:可定制性强(安装可以根据用户的需求,只安装所需要的功能)
缺点:安装复杂,所需要的时间比二进制的安装要长得多

3、构建自己的二进制rpm包
优点:根据需求定制成自己的rpm包,方便安装维护。
缺点:前期构建耗时较长,相对比较复杂

MySQL国内镜像下载地址:http://mirrors.sohu.com/mysql/

http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/

开源镜像站点汇总

http://mirrors.ustc.edu.cn/

二进制的rpm安装(红帽自带)

一、redhat mysql(RPM)        光盘中mysql老旧,漏洞多,如果一定要使用此版本,请到红帽的源码目录树中下载最新版本
# yum list|grep ^mysql
mysql.x86_64                     客户端
mysql-bench.x86_64               压力测试工具包
mysql-connector-java.noarch      连接器
mysql-connector-odbc.x86_64  
mysql-devel.i686                 开发包
mysql-devel.x86_64           
mysql-libs.i686                  库包(*.dll),可以让其他第三方程序调用这些库文件,扩充软件功能
mysql-libs.x86_64            
mysql-server.x86_64              服务器 
mysql-test.x86_64                测试库

安装服务端和客户端软件:
# yum -y install mysql mysql-server  

启动服务:
mysql启动原理
        执行mysqld(服务端命)            
        ----> 根据参数读取配置文件或者直接给命令传递参数
        ----> 读取相应的数据文件和其他的物理文件(日志文件等)
        ----> 生成socket文件和在相应的端口上进行监听
说明:rhel系统自动的数据库的启动脚本会判断数据目录是否为空,如果为空,它会自动初始化

# service mysqld start


测试是否启动ok:
# lsof -i:3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  7321 mysql   10u  IPv4  54236      0t0  TCP *:mysql (LISTEN)
# netstat -nltp|grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      7321/mysqld         

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.71 Source distribution

mysql> show databases;   查看有哪些库
+--------------------+
| Database           |
+--------------------+
| information_schema |      
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

information_schema数据库:
对象信息数据库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息。在INFORMATION_SCHEMA中,有数个只读表。它们实际上是视图,而不是基本表,因此,你将无法看到与之相关的任何文件。
视图:是一个虚表,即视图所对应的数据不进行实际存储,数据库中只存储视图的定义,在对视图的数据进行操作时,系统根据视图的定义去操作与视图相关联的基本表。
mysql数据库:
这个是mysql的核心数据库,主要负责存储数据库的用户、权限设置、关键字等mysql自己需要使用的控制和管理信息;不可以删除,也不要轻易修改这个数据库里面的表息。
test数据库:
这个是安装时候创建的一个测试数据库,和它的名字一样,是一个完全的空数据库,没有任何表,可以删除。

默认读取的首选配置文件 /etc/my.cnf
# cat /etc/my.cnf 
[mysqld]        用中括号括起来的叫参数组,用来针对不同的工具设定参数
datadir=/var/lib/mysql        数据文件存放的目录
socket=/var/lib/mysql/mysql.sock        socket文件是用于本地连接mysql数据库的接口文件,远程连接的话就要通过TCP/IP协议
user=mysql            管理mysql的系统用户
# Disabling symbolic-links is recommended to prevent assorted security risks  禁止使用符号链接,防止安全隐患
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log               # 错误日志文件
pid-file=/var/run/mysqld/mysqld.pid       # pid文件

注意:pid文件和socket文件是服务启动成功后才会有的

 官方rpm包安装

 

二、mysql AB (RPM)    -mysql官方的RPM包,提供版本比较多,像suse/redhat/oracle linux

MySQL-client-5.6.19-1.el6.x86_64.rpm   客户端
MySQL-devel-5.6.19-1.el6.x86_64.rpm    开发工具包(其他软件需要调用到mysql的头文件或者库文件时安装)eg:proxy、监控软件等
MySQL-embedded-5.6.19-1.el6.x86_64.rpm   嵌入式工具包
MySQL-server-5.6.19-1.el6.x86_64.rpm     服务端
MySQL-shared-5.6.19-1.el6.x86_64.rpm     工具包
MySQL-shared-compat-5.6.19-1.el6.x86_64.rpm
MySQL-test-5.6.19-1.el6.x86_64.rpm  测试库    

安装服务端和客户端:
1> 卸载掉已经安装的mysql

# rpm -aq|grep mysql|xargs rpm -e --nodeps
# rm -rf /var/lib/mysql/*
或者
# rpm -e `rpm -aq|grep mysql` --nodeps
2> 安装5.6.19rpm包

# rpm -ivh MySQL-client-5.6.19-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]
# rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]

# cat /root/.mysql_secret   默认root用户的密码
# The random password set for the root user at Wed Aug 16 11:10:58 2017 (local time): KTVnqzD2lUVvMBYP


# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

原因:数据库没有启动
解决:启动数据库

# /usr/bin/mysql_secure_installation 对数据库做安全配置



NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y  是否更改mysql数据库的root密码
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y   是否移除匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n  是否禁止root用户远程登录|生产禁止  
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y  移除test库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  刷新授权表
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...



启动数据库:
# service mysql start
Starting MySQL....                                         [  OK  ]

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

说明:
从mysql的5.1版本后,官方所发布的mysql的服务名称发生了变化,以前是mysqld现在是mysql,但是redhat 发行版本中依然是mysqld。

安全配置:
# mysql_secure_installation 

说明:默认情况下mysql数据库安装在/usr下;数据文件在/var/lib/mysql下

双版本安装

参考:https://www.cnblogs.com/yanjieli/p/9777466.html

glibc二进制包安装

参考:https://www.cnblogs.com/yanjieli/p/11950100.html

yum源安装

#下载mysql源安装包
[root@7ab22243eaf9 /]# wget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm

#安装mysql源
[root@7ab22243eaf9 /]# yum -y localinstall mysql57-community-release-el6-8.noarch.rpm

#检查mysql源是否安装成功
[root@7ab22243eaf9 /]# yum repolist enabled |grep "mysql.*-community.*"

#改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。
[root@7ab22243eaf9 /]# vim /etc/yum.repos.d/mysql-community.repo    #这一步可以省略,默认是安装最高版本
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


#安装mysql
[root@7ab22243eaf9 /]# yum -y install mysql-community-server

#启动mysql并设置开机自启动
[root@7ab22243eaf9 /]# service mysqld start 
Initializing MySQL database:                               [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@7ab22243eaf9 /]# chkconfig --add mysqld
[root@7ab22243eaf9 /]# chkconfig --level 35 mysqld on

#查看初始密码,存放在/var/log/mysqld.log文件里面
[root@7ab22243eaf9 /]# grep "temporary password" /var/log/mysqld.log 
2019-11-22T06:54:39.570807Z 1 [Note] A temporary password is generated for root@localhost: Rw)6ImidXYGq

#连接mysql进行修改密码
[root@7ab22243eaf9 /]# mysql -p
Enter password: Rw)6ImidXYGq    #这里输入上面的密码
mysql> alter user 'root'@'localhost' identified by "MyNewP@sswd1";
Query OK, 0 rows affected (0.00 sec)
mysql> \q

#使用新密码进行测试
[root@7ab22243eaf9 /]# mysql -pMyNewP@sswd1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
#下载mysql源安装包
[root@2a2d63b53730 /]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

#安装mysql源
[root@2a2d63b53730 /]# yum -y localinstall mysql57-community-release-el7-8.noarch.rpm

#检查mysql源是否安装成功
[root@2a2d63b53730 /]# yum repolist enabled |grep "mysql.*-community.*"

#安装mysql
[root@2a2d63b53730 /]# yum -y install mysql-community-server

#启动mysql并设置开机自启动
[root@2a2d63b53730 /]# systemctl enable mysqld
[root@2a2d63b53730 /]# systemctl start mysqld 

#查看初始密码,存放在/var/log/mysqld.log文件里面
[root@2a2d63b53730 /]# grep "temporary password" /var/log/mysqld.log 
2019-11-22T08:07:50.088821Z 1 [Note] A temporary password is generated for root@localhost: 9HveX=1pp2&o

#连接mysql进行修改密码
[root@2a2d63b53730 /]# mysql -p
Enter password: 9HveX=1pp2&o    #输入上面的密码
mysql> alter user 'root'@'localhost' identified by "MyNewP@sswd1";
Query OK, 0 rows affected (0.01 sec)
mysql> \q

#使用新密码进行测试
[root@2a2d63b53730 /]# mysql -pMyNewP@sswd1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

源码包编译安装

4、源码包安装 5.6.25
install_dir: /mysql25
data_dir: /mysql25/data
socket:/mysql25/mysql.sock
port:3308
config_file:/mysql25/etc/my.cnf

# md5sum mysql-5.6.25.tar.gz 
37664399c91021abe070faa700ecd0ed  mysql-5.6.25.tar.gz

步骤:
1、创建相应的数据目录
mkdir /mysql25/data -p

2、解压相应的软件包到指定的临时目录里(/usr/src)
# tar -xf mysql-5.6.25.tar.gz -C /usr/src/
# cd /usr/src/mysql-5.6.25/

3、根据需求进行配置
参考官方文档
默认情况下载当前目录下直接可以配置
shell> cmake . -LAH  查看所有支持的配置选项
shell> cmake .

CMAKE_INSTALL_PREFIX  /usr/local/mysql      安装路径
DEFAULT_CHARSET  latin1      拉丁 默认字符集
DEFAULT_COLLATION    latin1_swedish_ci     指定服务器默认的校对规则(排序规则)
ENABLED_LOCAL_INFILE    OFF     是否开启内部加载外部文件功能  默认off  1代表开启0代表关闭
MYSQL_DATADIR                     数据文件目录
SYSCONFDIR                           初始化参数文件目录(主配置文件路径(默认优先去/etc目录去找))
MYSQL_TCP_PORT                    服务端口号,默认3306
MYSQL_UNIX_ADDR                   socket文件路径,默认/tmp/mysql.sock 
WITHOUT_xxx_STORAGE_ENGINE        指定不编译的存储引擎
WITH_xxx_STORAGE_ENGINE       指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。
WITH_EXTRA_CHARSETS    扩展字符集

字符集与字符编码
字符是各种文字和符号的总称,包括各个国家文字、标点符号、图形符号、数字等。字符集是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同,
计算机要准确的处理各种字符集文字,需要进行字符编码,以便计算机能够识别和存储各种文字。也就是说字符编码是字符集的实现方式。
但需要注意的是:有的字符编码和字符集的名称是一致的。

常见的字符集
    Unicode:也叫统一字符集,它包含了几乎世界上所有的已经发现且需要使用的字符(如中文、日文、英文、德文等)。
    ASCII:早期的计算机系统只能处理英文,所以ASCII也就成为了计算机的缺省字符集,包含了英文所需要的所有字符。
    GB2312:中文字符集,包含ASCII字符集。ASCII部分用单字节表示,剩余部分用双字节表示。
    GBK:GB2312的扩展,但完整包含了GB2312的所有内容。
    GB18030:GBK字符集的超集,常叫大汉字字符集,也叫CJK(Chinese,Japanese,Korea)字符集,包含了中、日、韩三国语言中的所有字符。
常见的字符编码:
UTF-8 UTF-16 UCS-2 UCS-4 
GBK/GB2312
GB18030


字符编码        每个字符字节数
ASCII             1
UCS-2(Unicode)    2
UCS-4(Unicode)     4
UTF-8(Unicode)    1 - 6
UTF-16(Unicode)     2 - 4
GBK/GB2312(中文)     1 - 2
GB18030(CJK)        1- 4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

编写cmake脚本:
vim cmake.sh
#!/bin/bash
cmake . \
-DCMAKE_INSTALL_PREFIX=/mysql25 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/mysql25/data \
-DSYSCONFDIR=/mysql25/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/mysql25/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3308


chmod +x cmake.sh

# ./cmake.sh 
./cmake.sh: line 2: cmake: command not found

yum -y install cmake
# ./cmake.sh
-- Could NOT find Curses  (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):

# yum -y install ncurses-devel

说明:如果第一次配置出错,再次配置前删掉上一次的缓存文件
rm -f  CMakeCache.txt

4、编译
指定cpu个数编译
cat /proc/cpuinfo |grep processor|wc -l
make -j 2--------------------------指定多个CPU快速表编译

5、安装
make install

6、后续配置

初始化数据库(安装默认的库和表):
cd /mysql25

# ./mysql_install_db --help
--no-defaults:不要从任何的配置文件中读取相应的参数,忽略掉mysql安装过程中的默认配置,如创建默认用户并设置默认密码等

# ./mysql_install_db --basedir=/mysql25 --datadir=/mysql25/data --no-defaults --user=mysql 

启动数据库:
报错:
# service mysql25 start
Starting MySQL.The server quit without updating PID file (/mysql/data/vm1.uplook.com.pid).[FAILED]
解决:
chown mysql.mysql /mysql -R

环境变量:
export PATH=$PATH:/usr/local/mysql/bin

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@vm1 data]# mysql --socket=/data/mysql.sock 


# ln -s /data/mysql.sock /var/lib/mysql/mysql.sock

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25 Source distribution

Copyright (c) 2000, 2014, 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> exit
Bye


##############################################################################

总结:
1、修改配置文件重新指定了pid文件的路径
2、mysql的数据目录的权限问题或者pid文件的权限
3、mysql数据库没有正常关闭导致


5.7.17安装:
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chmod 750 mysql-files
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db --user=mysql    # MySQL 5.7.5
shell> bin/mysqld --initialize --user=mysql # MySQL 5.7.6 and up
shell> bin/mysql_ssl_rsa_setup              # MySQL 5.7.6 and up
shell> chown -R root .
shell> chown -R mysql data mysql-files
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
1. 编译安装
[root@mysql1 ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make

cmake:

mysql:
[root@mysql1 ~]# groupadd mysql
[root@mysql1 ~]# useradd -r -g mysql -s /bin/false mysql
[root@mysql1 ~]# tar xvf mysql-5.7.17.tar.gz
[root@mysql1 ~]# cd mysql-5.7.17
[root@mysql-5.7.17 ~]# cmake . \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

[root@mysql1 ~]# make
[root@mysql1 ~]# make install

2. 初始化
[root@mysql1 local]# cd mysql
[root@mysql1 mysql]# chown -R mysql .
[root@mysql1 mysql]# chgrp -R mysql .
[root@mysql1 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@mysql1 mysql]# bin/mysql_ssl_rsa_setup
[root@mysql1 mysql]# chown -R root .
[root@mysql1 mysql]# chown -R mysql data mysql-files
[root@mysql1 mysql]# \cp -rf support-files/my-default.cnf  /etc/my.cnf
[root@mysql1 mysql]# bin/mysqld_safe --user=mysql &
[root@mysql1 mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql1 mysql]# source /etc/profile
[root@mysql1 mysql]# mysql -uroot -p'xxxx'
mysql> alter user root@'localhost' identified by 'tianyun';
MySQL5.6.31源码安装
下载链接:https://pan.baidu.com/s/1yPGylgq-BbiGwZr-QN4ztw 密码:18dc

安装前的准备:
创建用户:
[root@Admin ~]# groupadd mysql && useradd mysql -g mysql -s /bin/false

# 创建数据目录:
[root@Admin ~]# mkdir -p /data/DB

# 安装必备软件:
[root@Admin ~]# yum install unzip cmake ncurses-devel gcc gcc-c++ -y

# 安装mysql
[root@Admin ~]# unzip mysql-5.6.31.zip && cd mysql-5.6.31

[root@Admin mysql-5.6.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var/ -DWITH_DEBUG=0 -DEXTRA_CHARSETS=latin1,gb2312,big5,utf8,GBK -DEXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1

[root@Admin mysql-5.6.31]# make && make install 

[root@Admin mysql-5.6.31]# cp support-files/mysql.server /etc/init.d/mysqld

# 编写配置文件
[root@Admin mysql-5.6.31]# vim /etc/my.cnf
[client]
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8

[mysqld]
port=3306
lower_case_table_names=1                        #大小写区分表明
thread_concurrency=64                         #通过比较 Connections 和 Threads_created 状态的变量
slow_query_log=1
slow_query_log_file=/data/mysqldata/slowquery.log    #为MySQL慢查询日志存放的位置
long_query_time=1                                #2表示查询超过两秒才记录
character-set-server=utf8        
explicit_defaults_for_timestamp                  #开查询缓存

socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_buffer_pool_size=50M
innodb_additional_mem_pool=16M
log-bin=mysql-bin
datadir=/data/DB

log-bin-trust-function-creators=1

lower_case_table_names=1
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
binlog_format=mixed

server-id = 1

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


# 配置,初始化
[root@Admin mysql-5.6.31]# chmod +x scripts/mysql_install_db && chmod +x /etc/init.d/mysqld

[root@Admin mysql-5.6.31]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/DB/

[root@Admin mysql-5.6.31]# chkconfig --add mysqld && chkconfig --level 35 mysqld on

[root@Admin mysql-5.6.31]# cp /usr/local/mysql/bin/mysql /usr/bin/ && cp /usr/local/mysql/bin/mysqldump /usr/bin/ && cp /usr/local/mysql/bin/mysqladmin /usr/bin/

# 启动并进入
[root@Admin mysql-5.6.31]# service mysqld restart 
MySQL server PID file could not be found!                  [失败]
Starting MySQL.                                            [确定]
[root@Admin mysql-5.6.31]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.31-log Source distribution

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>

MySQL 后续配置

密码设置

1》密码设置        新建立的数据库默认管理员帐号是没有密码,任何人都可以连接
修改密码三种方法:
第一种:
# mysqladmin -u root password "123"    --把root用户登录密码改为123
# mysqladmin -u root password 'newpasswd' -p123    --有密码后,再使用这个命令改密码,就需要原密码

第二种:
使用sql语句在数据库内部直接修改用户密码表,password()是一个密码加密函数
mysql> update mysql.user set password=password("789") where user="root" and host="localhost"; 
或者
mysql> use mysql;
mysql> update user set password=password(123) where user='root' and host='localhost';    
mysql> flush privileges;    --修改过密码后都要记得刷新权限表

第三种:
mysql> set password for 'root'@'localhost'=password('123'); --使用此操作语句也可以修改密码,修改后不需要刷新权限表

忘记密码

# 忘记密码怎么办?
方法1:
修改配置文件,在配置文件里面添加跳过授权表
skip-grant-tables

方法2:
通过mysqld_safe 命令启动传递参数
--skip-grant-tables

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注