MySQL 是最盛行的联系型数据库办理系统,由瑞典MySQLAB公司开发,现在归于Oracle公司。

MySQL所运用的SQL语言是用于访问数据库的最常用标准化语言。

MySQL因为其体积小、速度快、整体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都挑选MySQL作为网站数据库。

MySQL 配置

1、创立mysql运行运用的用户mysql:

$ /usr/sbin/groupadd mysql
$ /usr/sbin/useradd -g mysql mysql

2、创立binlog和库的存储路径并赋予mysql用户权限

$ mkdir -p /usr/local/webserver/mysql/binlog /www/data_mysql
$ chown mysql.mysql /usr/local/webserver/mysql/binlog/ /www/data_mysql/

3、创立my.cnf配置文件

将/etc/my.cnf替换为下面内容

$ cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /www/data_mysql
log-error = /usr/local/webserver/mysql/mysql_error.log
pid-file = /usr/local/webserver/mysql/mysql.pid
open_files_limit = 65535
back_log = 600
max_connections = 5000
max_connect_errors = 1000
table_open_cache = 1024
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 600
#thread_concurrency = 8
query_cache_size = 128M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
default-tmp-storage-engine=MYISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 128M
max_heap_table_size = 128M
log-slave-updates
log-bin = /usr/local/webserver/mysql/binlog/binlog
binlog-do-db=oa_fb
binlog-ignore-db=mysql
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /usr/local/webserver/mysql/relaylog/relaylog
relay-log-info-file = /usr/local/webserver/mysql/relaylog/relaylog
relay-log = /usr/local/webserver/mysql/relaylog/relaylog
expire_logs_days = 10
key_buffer_size = 256M
read_buffer_size = 1M
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
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.2
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
loose-innodb-trx=0
loose-innodb-locks=0
loose-innodb-lock-waits=0
loose-innodb-cmp=0
loose-innodb-cmp-per-index=0
loose-innodb-cmp-per-index-reset=0
loose-innodb-cmp-reset=0
loose-innodb-cmpmem=0
loose-innodb-cmpmem-reset=0
loose-innodb-buffer-page=0
loose-innodb-buffer-page-lru=0
loose-innodb-buffer-pool-stats=0
loose-innodb-metrics=0
loose-innodb-ft-default-stopword=0
loose-innodb-ft-inserted=0
loose-innodb-ft-deleted=0
loose-innodb-ft-being-deleted=0
loose-innodb-ft-config=0
loose-innodb-ft-index-cache=0
loose-innodb-ft-index-table=0
loose-innodb-sys-tables=0
loose-innodb-sys-tablestats=0
loose-innodb-sys-indexes=0
loose-innodb-sys-columns=0
loose-innodb-sys-fields=0
loose-innodb-sys-foreign=0
loose-innodb-sys-foreign-cols=0
slow_query_log_file=/usr/local/webserver/mysql/mysql_slow.log
long_query_time = 1
[mysqldump]
quick
max_allowed_packet = 32M

4、初始化数据库

$/usr/local/webserver/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql

显现如下信息:

Installing MySQL system tables...2015-01-26 20:18:51 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
OK
Filling help tables...2015-01-26 20:18:57 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
OK
...

5、创立开机发动脚本

$ cd /usr/local/webserver/mysql/
$ cp support-files/mysql.server /etc/rc.d/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig --level 35 mysqld on

6、发动mysql服务器

$ service mysqld start

MySQL 安装配置

7、衔接 MySQL

$ /usr/local/webserver/mysql/bin/mysql -u root -p

MySQL 安装配置

修改MySQL用户暗码

mysqladmin -u用户名 -p旧暗码 password 新暗码

或进入mysql指令行

SET PASSWORD FOR '用户名'@'主机' = PASSWORD(‘暗码');

创立新用户并授权:

grant all privileges on *.* to 用户名@'%' identified by '暗码' with grant option;

其他指令

  • 发动:service mysqld start
  • 中止:service mysqld stop
  • 重启:service mysqldrestart
  • 重载配置:service mysqldreload

发动MySQL服务器程序,承认状况

1)发动MySQL服务程序

发动服务并检查状况:

[root@dbsvr1 pub]# service mysql start
Starting MySQL...                                     [确定]
[root@dbsvr1 pub]# service mysql status
MySQL running (31724)                                 [确定]

服务器进程为mysqld,监听的默许端口为TCP 3306:

[root@dbsvr1 pub]# netstat -anpt | grep mysql
tcp        0      0 :::3306        :::*       LISTEN      31724/mysqld

2)检查MySQL服务器进程、运行用户

供给衔接服务的进程为mysqld,由其父进程mysqld_safe发动。

[root@dbsvr1 pub]# ps -elf | grep mysqld
4 S root     31619     1  0  80   0 -  2834 wait   15:14 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid file=/var/lib/mysql/dbsvr1.tarena.com.pid
4 S mysql    31724 31619  0  80   0 - 252496 poll_s 15:14 pts/0   00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/dbsvr1.tarena.com.err --pid-file=/var/lib/mysql/dbsvr1.tarena.com.pid

数据库的默许寄存方位为 /var/lib/mysql:

[root@dbsvr1 pub]# ls /var/lib/mysql/
auto.cnf               ibdata1      mysql               RPM_UPGRADE_HISTORY
dbsvr1.tarena.com.err  ib_logfile0  mysql.sock          RPM_UPGRADE_MARKER-LAST
dbsvr1.tarena.com.pid  ib_logfile1  performance_schema  test