一.简介
数据库(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 mysqlservice mysqld start2、建立配置文件/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 mysqlshell> useradd -r -g mysql mysqlshell> cd /optshell> ln -s full-path-to-mysql-VERSION-OS mysqlshell> cd mysqlshell> chown -R mysql .shell> chgrp -R mysql .shell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql datashell> cp support-files/my-medium.cnf /etc/my.cnfshell> bin/mysqld_safe --user=mysql &shell> cp support-files/mysql.server /etc/init.d/mysql.glbic# vim /etc/init.d/mysql.glibcbasedir=/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-sourcedatadir=/opt/mysql-source/var4.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 = 3307socket = /usr/local/mysql/sockets/mysqld.sock[mysqld]port = 3307socket = /usr/local/mysql/sockets/mysqld.socklog-error=/usr/local/mysql/mysql_error.logdatadir=/usr/local/mysql/varrelay-log = /usr/local/mysql/var/slave-relay.logrelay-log-info-file=/usr/local/mysql/var/relay-log.inforelay-log-index =/usr/local/mysql/var/relay-log.indexskip-external-lockingback_log = 50skip-name-resolvemax_connections =2500max_connect_errors = 1000auto-increment-increment = 2auto-increment-offset = 1table_open_cache = 2048max_allowed_packet = 1024Mbinlog_cache_size = 1Mmax_heap_table_size = 1024Msort_buffer_size = 32Mjoin_buffer_size = 8Mthread_cache_size = 16query_cache_size = 16Mquery_cache_limit = 2Mft_min_word_len = 4default-storage-engine = INNODBtransaction_isolation = REPEATABLE-READtmp_table_size = 512Mlog-bin=mysql-binbinlog_format=mixedexpire_logs_days = 7slow_query_loglong_query_time = 2server-id = 2key_buffer_size = 256Mread_buffer_size = 8Mread_rnd_buffer_size = 16Mbulk_insert_buffer_size = 64Mmyisam_sort_buffer_size = 128Mmyisam_max_sort_file_size = 10Gmyisam_repair_threads = 1myisam_recoverinnodb_file_per_tableinnodb_data_home_dir = /usr/local/mysql/varinnodb_additional_mem_pool_size = 16Minnodb_buffer_pool_size = 1Ginnodb_data_file_path = ibdata1:10M:autoextendinnodb_log_group_home_dir = /usr/local/mysql/var#innodb_file_io_threads = 48innodb_read_io_threads =8innodb_write_io_threads =8innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 8Minnodb_log_file_size = 256Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_flush_method = O_DIRECT[mysqldump]quickmax_allowed_packet = 1024M[mysql]no-auto-rehash[myisamchk]key_buffer_size = 512Msort_buffer_size = 512Mread_buffer = 8Mwrite_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 osimport sysimport pub_printlogSHELLSTRING = '''#!/bin/bash#安装软件存放路径 %s#软件安装目录 %s#软件数据存放目录 %s#软件日志存放目录 %s#配置文件存放目录 /etc/my.cnf#sock文件存放目录 /tmp/mysql.sock#假设安装时间2015年09月09日15时#此台服务器为当日安装的第一台服务器 server-id=2015090901#创建mysql用户id mysqlgroupadd -g 525 mysqluseradd -g mysql -m -d /home/mysql -u 525 mysql#设置mysql用户密码passwd --stdin mysql << 'MUL'%sMULcd %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/ mysqlcd mysql/#创建数据目录及数据日志存放目录mkdir -p %smkdir -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= 3306socket= /tmp/mysql.sock[mysqld]##general configurationserver-id = %s#bind-address = %suser=mysqlport=3306basedir=%sdatadir=%ssocket=/tmp/mysql.sockpid-file=%s/mysql.pidevent_scheduler = 0lower_case_table_names=1character-set-server = utf8transaction-isolation = REPEATABLE-READskip_name_resolvemax_connect_errors = 100000skip-external-lockinginnodb_file_per_table = 1innodb_data_home_dir = %sinnodb_data_file_path= ibdata1:1G:autoextendinnodb_log_group_home_dir = %sinnodb_buffer_pool_size= %sinnodb_log_files_in_group = 3innodb_log_file_size = 256Minnodb_log_buffer_size = 32Minnodb_flush_log_at_trx_commit = 2innodb_lock_wait_timeout = 50key_buffer_size = 128Mbulk_insert_buffer_size = 32Mmyisam_sort_buffer_size = 128Mmyisam_max_sort_file_size = 10Gread_buffer_size = 2Mread_rnd_buffer_size = 8Msort_buffer_size = 4Mjoin_buffer_size = 8Mmax_connections = 2048open_files_limit = 65535table_open_cache = 512tmp_table_size = 256Mmax_heap_table_size = 256Mquery_cache_type = 1query_cache_size = 512Mquery_cache_limit = 2Mquery_cache_min_res_unit = 512thread_cache_size = 1024thread_stack = 256Kslow_query_log=1slow_query_log_file = %s/mysql-slow.loglong_query_time = 1log-error = %s/mysql-error-log.errlog_warnings = 2log-bin = %s/mysql-binbinlog_format = rowmax_binlog_size = 1Gbinlog_cache_size = 4Mmax_binlog_cache_size = 2Gsync_binlog = 0expire_logs_days = 7##bestpay secure configurationsql_mode=NO_AUTO_CREATE_USERlocal_infile=offsecure_authMUL#将数据目录的属主更改为mysql,否则会导致数据初始化失败 chown -R mysql.mysql /mysql/./scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql &sleep 180ls %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=%sexport PATH=$PATH:$MYSQL_HOME/binMULsource /etc/profile#修改文件属主及权限chown mysql.mysql /etc/my.cnfchmod 644 /etc/my.cnfcp %s/support-files/mysql.server /etc/init.d/mysqldchown root.root /etc/init.d/mysqldchmod 755 /etc/init.d/mysqld chown -R mysql.mysql %schmod 755 %schown -R mysql.mysql %s/chmod 755 %s/#启动mysql服务service mysqld start#设置服务自启动cat>>/etc/rc.local<<'MUL'/etc/init.d/mysqld startMUL#安装完毕!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('\n', '').replace('\r', '') 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', '运行结束。')