一.简介
数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,
每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据。
我们也可以将数据存储在文件中,但是在文件中读写数据速度相对较慢。
所以,现在我们使用关系型数据库管理系统(RDBMS)来存储和管理的大数据量。所谓的关系型数据库,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。
RDBMS即关系数据库管理系统(Relational Database Management System)的特点:
- 1.数据以表格的形式出现
- 2.每行为各种记录名称
- 3.每列为记录名称所对应的数据域
- 4.许多的行和列组成一张表单
- 5.若干的表单组成database
二.安装环境搭建
四种方法
1、通过二进制的方式安装
1) rpm(redhat/mysql rpm)
2) mysql glbic
优点:安装和维护都比较方便,不需要编译。
缺点:可定制性差,可移植性差,一个系统上只能安装一个mysql
2、通过源代码编译的安装(mysql-xx.tar.gz)
5.1源码安装
5.5源码安装
优点:可定制性强(安装可以根据用户的需求,只安装所需要的功能)
缺点:安装复杂,所需要的时候比二进制的安装要长得多
2.1 二进制安装
1、redhat RPM
# yum list |grep mysql
mysql.x86_64 --客户端
mysql-libs.x86_64 --库包(类似于dll),可以让第三方程序调用这些库文件,扩充软件的功能。
mysql-server.x86_64 --服务器软件包
mysql-bench.x86_64 --压力测试工具包
mysql-connector-odbc.x86_64 --连接器
mysql-devel.x86_64 --开发包
mysql-test.x86_64 --测试数据库包
1、安装软件
rpm -ivh mysqlrpm or
#yum -y install mysql-server mysql
service mysqld start
2、建立配置文件/etc/my.cnf
# vim /etc/my.cnf
[mysqld] <---[进程名字] 对应的配置给予对应的进程名字的进程使用,mysqld是服务端
datadir=/data <---数据库用于存放数据的数据文件所在路径
socket=/data/mysqld.sock <---套接字文件,服务启动的时候生成,服务正常关闭就删除该文件
user=mysql <--- 服务运行的有效身份
port=3306 <--- tcp协议监听的端口
2.glibc安装
# service mysql stop --先停止RPM包安装的mysqld
# tar xzvf mysql-5.5.25-linux2.6-x86_64_glibc.tar.gz -C /opt
参照安装文档
INSTALL-BINARY
******************************************************
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /opt
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
shell> cp support-files/mysql.server /etc/init.d/mysql.glbic
# vim /etc/init.d/mysql.glibc
basedir=/opt/mysql-glibc
*******************************************
3.源码安装5.1
# pkill mysql
# tar xzvf mysql-5.1.58.tar.gz -C /usr/local/src/
# cd /usr/local/src/mysql-5.1.58/
# ./configure --prefix=/opt/mysql-source --with-big-tables --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-charset=utf8 --with-extra-charsets=all
--with-big-tables 64系统支持4G的大表
--with-mysqld-ldflags=-all-static 服务端使用静态库的方式编译
--with-client-ldflags=-all-static 客户端使用表态库的方式编译官方称可提升mysql5%-10%左右的性能,静态编译就是把程序要用到的一些库文件给编译到程序里,那么调用方便,当然编译 软件的体积会大一点。
--with-charset=utf8 --指定默认的语言编码utf8
--with-extra-charsets=all --指定扩展的语言编码
# make && make install
# cd /opt/mysql-source/
# chown mysql.mysql . -R
# mv /etc/my.cnf /etc/my.cnf.bak.bak
# ./bin/mysql_install_db --user=mysql
# chown root . -R
# chown mysql var -R
# cp share/mysql/mysql.server /etc/init.d/mysql.souce
# vim /etc/init.d/mysql.souce
basedir=/opt/mysql-source
datadir=/opt/mysql-source/var
4.C语言编译安装
1.安装cmake
# tar xzf cmake-2.8.7.tar.gz –C /usr/local
# cd /usr/local/cmake-2.8.7
# ./configure && make &&make install
2.安装mysql
shell> cmake . -L # overview等于./confgiure --help
shell> cmake . -LH # 查看简约的帮助,相比上一步有更详细一点的选项说明
shell> cmake . -LAH # 查看所有的配置选项,包括选项的解释说明
shell> ccmake . #伪图形界来配置软件
shell> tar zxf mysql-5.5.28.tar.gz –C /usr/local/
shell> cd /usr/local/mysql-5.5.28
shell> cmake . -DCMAKE_INSTALL_PREFIX=/usr/src/mysql --指定安装路径
-DWITH_INNOBASE_STORAGE_ENGINE=1 --启用innodb存储引擎
-DENABLED_LOCAL_INFILE=1 --允许通过本地导入数据
-DDEFAULT_CHARSET=utf8 --指定默认的语言编码
-DEXTRA_CHARSETS=all --扩展语言编码
-DDEFAULT_COLLATION=utf8_general_ci --排序语言编码
-DSYSCONFDIR=/usr/src/mysql/etc --配置文件的目录
-DMYSQL_DATADIR=/data --数据目录
-DMYSQL_UNIX_ADDR=/data/mysql.sock --socket目录
shell> make && make install
shell> cd /opt/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts /mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell>cp /etc/my.cnf /etc/my.cnf.bak
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
shell>netstat –antpl |grep mysqld ---检验服务是否开启
3.更改配置生成mysql启动脚本
# cp support-files/mysql.server /etc/init.d/mysql.server
# ls /etc/init.d |find mys
# mv /etc/init.d/mysql.server /etc/init.d/mysqld
# vi /etc/init.d/mysqld
测试mysql文件是否生效
# service mysqld restart
4.将mysql服务实现开机自启动
# chkconfig mysqld on
三.安装完成后环境配置
3.1.数据库基础配置,创建维护账号
select user,host,password from mysql.user; --查看数据库用户信息
create user 'test'@'%' identified by 'CFVIRTUAL3303'; --创建远程用户
create user 'test'@'localhost' identified by 'CFVIRTUAL3303'; --创建本地用户
grant all privileges on *.* to 'test'@'%' identified by 'password' WITH GRANT OPTION; --授权用户所有数据库权限
flush privileges;
update user set password='password' where user='yuming'; --更改用户密码
grant all privileges on mobiledb.* to 'test'@'%' WITH GRANT OPTION; --授权某个用户指定数据库
flush privileges; ---刷新数据库权限
3.2 调整my.cnf 的默认配置参数
# vim /usr/local/mysql/etc/my.cnf 文件内容如下: [client] port = 3307 socket = /usr/local/mysql/sockets/mysqld.sock [mysqld] port = 3307 socket = /usr/local/mysql/sockets/mysqld.sock log-error=/usr/local/mysql/mysql_error.log datadir=/usr/local/mysql/var relay-log = /usr/local/mysql/var/slave-relay.log relay-log-info-file=/usr/local/mysql/var/relay-log.info relay-log-index =/usr/local/mysql/var/relay-log.index skip-external-locking back_log = 50 skip-name-resolve max_connections =2500 max_connect_errors = 1000 auto-increment-increment = 2 auto-increment-offset = 1 table_open_cache = 2048 max_allowed_packet = 1024M binlog_cache_size = 1M max_heap_table_size = 1024M sort_buffer_size = 32M join_buffer_size = 8M thread_cache_size = 16 query_cache_size = 16M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = INNODB transaction_isolation = REPEATABLE-READ tmp_table_size = 512M log-bin=mysql-bin binlog_format=mixed expire_logs_days = 7 slow_query_log long_query_time = 2 server-id = 2 key_buffer_size = 256M read_buffer_size = 8M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover innodb_file_per_table innodb_data_home_dir = /usr/local/mysql/var innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 1G innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /usr/local/mysql/var #innodb_file_io_threads = 48 innodb_read_io_threads =8 innodb_write_io_threads =8 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 8M innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_flush_method = O_DIRECT [mysqldump] quick max_allowed_packet = 1024M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192
#!/usr/bin/python # -*- coding: utf-8 -*- ################################################################################ # #Coding : utf-8 #FileName : ist_mysql.py #Desc : 自动化安装Mysql #call : python ist_mysql.py mysql用户密码 server-id 绑定IP地址 innodb缓冲区大小 安装软件存放路径 软件安装目录 软件数据存放目录 软件日志存放目录 # #example : python ist_mysql.py mysql@2015 2015110301 127.0.0.1 4G /usr/local/src /usr/local/mysql /mysql/data /mysql/logs ################################################################################ """this script for auto install mysql.""" import os import sys import pub_printlog SHELLSTRING = ''' #!/bin/bash #安装软件存放路径 %s #软件安装目录 %s #软件数据存放目录 %s #软件日志存放目录 %s #配置文件存放目录 /etc/my.cnf #sock文件存放目录 /tmp/mysql.sock #假设安装时间2015年09月09日15时 #此台服务器为当日安装的第一台服务器 server-id=2015090901 #创建mysql用户 id mysql groupadd -g 525 mysql useradd -g mysql -m -d /home/mysql -u 525 mysql #设置mysql用户密码 passwd --stdin mysql << 'MUL' %s MUL cd %s/ #解压安装 cd .. tar -zxvf %s/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.25-linux-glibc2.5-x86_64/ mysql cd mysql/ #创建数据目录及数据日志存放目录 mkdir -p %s mkdir -p %s #修改配置文件,里面对应的参数设置应根据系统配置做相应修改 cat >>/etc/my.cnf<<'MUL' [client] #comment those three rows when use mysqlbinlog. #------------------------------------------------- #prompt="\\u@\\h:\\d \\r:\\m:\\s>" #default-character-set=utf8 #no-auto-rehash #------------------------------------------------- port= 3306 socket= /tmp/mysql.sock [mysqld] ##general configuration server-id = %s #bind-address = %s user=mysql port=3306 basedir=%s datadir=%s socket=/tmp/mysql.sock pid-file=%s/mysql.pid event_scheduler = 0 lower_case_table_names=1 character-set-server = utf8 transaction-isolation = REPEATABLE-READ skip_name_resolve max_connect_errors = 100000 skip-external-locking innodb_file_per_table = 1 innodb_data_home_dir = %s innodb_data_file_path= ibdata1:1G:autoextend innodb_log_group_home_dir = %s innodb_buffer_pool_size= %s innodb_log_files_in_group = 3 innodb_log_file_size = 256M innodb_log_buffer_size = 32M innodb_flush_log_at_trx_commit = 2 innodb_lock_wait_timeout = 50 key_buffer_size = 128M bulk_insert_buffer_size = 32M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 4M join_buffer_size = 8M max_connections = 2048 open_files_limit = 65535 table_open_cache = 512 tmp_table_size = 256M max_heap_table_size = 256M query_cache_type = 1 query_cache_size = 512M query_cache_limit = 2M query_cache_min_res_unit = 512 thread_cache_size = 1024 thread_stack = 256K slow_query_log=1 slow_query_log_file = %s/mysql-slow.log long_query_time = 1 log-error = %s/mysql-error-log.err log_warnings = 2 log-bin = %s/mysql-bin binlog_format = row max_binlog_size = 1G binlog_cache_size = 4M max_binlog_cache_size = 2G sync_binlog = 0 expire_logs_days = 7 ##bestpay secure configuration sql_mode=NO_AUTO_CREATE_USER local_infile=off secure_auth MUL #将数据目录的属主更改为mysql,否则会导致数据初始化失败 chown -R mysql.mysql /mysql/ ./scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql & sleep 180 ls %s/mysql/ #重命名数据目录下的配置文件名,否则会导致部分/etc/my.cnf中的配置失效,命名规则为my+年份前两位+月日 mv my.cnf my`date +%%y%%m%%d`.cnf #检查对应的目录下初始化的数据是否正常 ls %s/ ls %s/ #设置PATH变量 cat>>/etc/profile<<'MUL' export MYSQL_HOME=%s export PATH=$PATH:$MYSQL_HOME/bin MUL source /etc/profile #修改文件属主及权限 chown mysql.mysql /etc/my.cnf chmod 644 /etc/my.cnf cp %s/support-files/mysql.server /etc/init.d/mysqld chown root.root /etc/init.d/mysqld chmod 755 /etc/init.d/mysqld chown -R mysql.mysql %s chmod 755 %s chown -R mysql.mysql %s/ chmod 755 %s/ #启动mysql服务 service mysqld start #设置服务自启动 cat>>/etc/rc.local<<'MUL' /etc/init.d/mysqld start MUL #安装完毕! echo 'mysql has installed! script exit.' ''' def write_file(filename, content): """this function write shell file.""" file_object = open(filename, 'w') try: file_object.write(content) finally: file_object.close() if __name__ == '__main__': pub_printlog.printalllog('ist_mysql', '开始运行。') if not len(sys.argv) == 9: pub_printlog.printlog('error', 'ist_mysql', '参数数量错误。目前只接收到%s个参数。' % (len(sys.argv))) print 'argv number wrong!' print '正确的参数传递应该为:python ist_mysql.py mysql用户密码 server-id 绑定IP地址 innodb缓冲区大小 安装软件存放路径 软件安装目录 软件数据存放目录 软件日志存放目录' pub_printlog.printalllog('ist_mysql', '运行结束。') exit() #产生SHELLSTRING脚本 pub_printlog.printlog('info', 'ist_mysql', '产生SHELLSTRING脚本。') write_file('/root/ist_mysql.sh', SHELLSTRING % (sys.argv[5], sys.argv[6], sys.argv[7], sys.argv[8], sys.argv[1], sys.argv[5], sys.argv[5], sys.argv[7], sys.argv[8], sys.argv[2], sys.argv[3], sys.argv[6], sys.argv[7], sys.argv[7], sys.argv[7], sys.argv[7], sys.argv[4], sys.argv[8], sys.argv[8], sys.argv[8], sys.argv[7], sys.argv[7], sys.argv[8], sys.argv[6], sys.argv[6], sys.argv[7], sys.argv[7], sys.argv[6], sys.argv[6] )) pub_printlog.printlog('info', 'ist_mysql', 'SHELLSTRING脚本生成完毕。') #修改权限 pub_printlog.printlog('info', 'ist_mysql', '修改权限。') os.system('chmod 774 /root/ist_mysql.sh') pub_printlog.printlog('info', 'ist_mysql', '修改权限完毕。') #执行脚本 pub_printlog.printlog('info', 'ist_mysql', '执行脚本。') os.system('/root/ist_mysql.sh') pub_printlog.printlog('info', 'ist_mysql', '执行脚本完毕。') #睡眠3分钟,等待mysql服务起来。最好的办法是进程检测。此处偷懒。 #pub_printlog.printlog('info', 'ist_mysql', '睡眠3分钟。') #time.sleep(180) #pub_printlog.printlog('info', 'ist_mysql', '睡眠3分钟结束。') #对帐号进行处理。 #修改root帐号密码。 #mysqladmin不识别prompt。只能用别的方法。去掉注释请增加import time模块的代码。 #os.system("/usr/local/mysql/bin/mysqladmin -u root password 'root'") #os.system("/usr/local/mysql/bin/mysqladmin -u root -h 127.0.0.1 password 'root'") pub_printlog.printlog('info', 'ist_mysql', '修改root帐号密码。') os.system('''%s/bin/mysql -u root -e "%s"''' % (sys.argv[6], "update mysql.user set password=password('root') where user='root';")) os.system('''%s/bin/mysql -u root -e "%s"''' % (sys.argv[6], "flush privileges;")) pub_printlog.printlog('info', 'ist_mysql', '修改root帐号密码完成。') #删除危险帐号。 pub_printlog.printlog('info', 'ist_mysql', '删除危险帐号。') pub_printlog.printlog('info', 'ist_mysql', '删除空名与::1主机帐号。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "delete from mysql.user where user='';")) os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "delete from mysql.user where host='::1';")) OUTPUT = os.popen('hostname') HOSTNAME = OUTPUT.read().replace(' ', '').replace(' ', '') pub_printlog.printlog('info', 'ist_mysql', '删除主机名帐号。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "delete from mysql.user where host = '%s';" % (HOSTNAME))) os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "flush privileges;")) pub_printlog.printlog('info', 'ist_mysql', '删除危险帐号完成。') #增加复制帐号,备份帐号,监控帐号。 pub_printlog.printlog('info', 'ist_mysql', '增加复制帐号。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "grant replication slave on *.* to replication@'172.%' identified by 'slave@2015';")) pub_printlog.printlog('info', 'ist_mysql', '增加备份帐号。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "grant select, reload, lock tables, replication client on *.* to 'backup'@'localhost' identified by 'backup@2015';")) pub_printlog.printlog('info', 'ist_mysql', '增加监控帐号。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "grant process,replication client on *.* to monitor@'localhost' identified by 'monitor@2015';")) #删除test库。 pub_printlog.printlog('info', 'ist_mysql', '删除test库。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "drop database test;")) pub_printlog.printlog('info', 'ist_mysql', '删除test库完成。') #修改root帐号名称。 pub_printlog.printlog('info', 'ist_mysql', '修改root帐号名称。') os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "update mysql.user set user='rootbp' where user='root';")) os.system('''%s/bin/mysql -u root -p'root' -e "%s"''' % (sys.argv[6], "flush privileges;")) pub_printlog.printlog('info', 'ist_mysql', '修改root帐号名称完成。') #删除临时文件 pub_printlog.printlog('info', 'ist_mysql', '删除临时文件。') os.system('rm -f /root/ist_mysql.sh') pub_printlog.printlog('info', 'ist_mysql', '删除临时文件完成。') print "user's privileges done!" pub_printlog.printalllog('ist_mysql', '运行结束。')