1.Mysql5.6安装前提
下载安装包:mysql.com
md5sum MySQL-5.6*.tar.gz 检查下载安装包有无问题
1.1关闭防火墙:
service iptables stop ;
chkconfig iptables off;
chkconfig --list |grep iptables
1.2关闭selinux:
vi /etc/sysconfig/selinux 改SELINUX=disabled
1.3建议I/O调度系统为deadline模式(默认为cfq模式)
vi /etc/grub.conf
elevator=deadline
1.4swap分区设置
swappiness值:
0:代表最大限度地使用物理内存,最后才使用swap分区 (内存溢出,kill MySQL)
100:积极使用swap分区 ,并把内存上面数据及时搬到swap分区里(不建议)
查看:cat /etc/sysctl.conf 加入vm.swappiness值即可
sysctl -a |grep swap
1.5建议文件系统类型为:xfs
1.6ulimit -a查看:
open files
max user processes (多实例
修改系统软,硬限制:
vi /ec/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
1.6,numa需要关闭,可以更好的分配内存,不采用swap方式来获得内存
numa --interleave=all /usr/local/mysql/bin/mysqld_safe -defaults-fie=/etc/my.cnf &
2.安装mysql5.6:
2.1添加用户:
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
mysqlbase(/usr/local/)
tar -zxvf mysql-5.6*.tar.gz
ln -sf mysql-5.6-*x86_64 mysql
chown mysql:mysql -R mysql
2.2创建数据库目录
mkdir -p /data/mysql
chown mysql:mysql /data/mysql -R
2.3安装后,配置参数
vi /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
open_files_limit = 65535
back_log = 103
max_connections = 512
max_connect_errors =100000
table_open_cache = 512
external-locking = FALSE
max_allowed_packet = 128M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51M
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
slow_query_log = 1
slow_query_log_file = /data/mysql/slow.log
log-error = /data/mysql/error.log
long_query_time = 0.5
server-id = 113306
log-bin = /data/mysql/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 128M
max_binlog_size = 1024M
expire_logs_days = 7
key_buffer_size = 32M
read_buffer_size = 1
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
character-set-server=utf8
default-storage-engine=InnoDB
binlog_format=row
#gtid_mode=on
#log_slave_updates=1
#enforce_gtid_consistency=1
interactive_timeout=300
wait_timeout=300
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1434M
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_locks_unsafe_for_binlog = 0
[mysqldump]
quick
max_allowed_packet = 32M
2.4 初始化数据库
cd /usr/local/mysql/scripts
./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=/myql
ok(2个时初始化成功)
cd /usr/local/mysql/bin
./mysqld_safe --defaults-file=/etc/my.cnf & (启动数据库)
ps -ef |grep mysql
mysql读取配置文件顺序:/etc/my.cnf-->/etc/mysql/my.cnf-->/usr/local/mysql/etc/my.cnf~/.my.cnf
创建密码:
use mysql ;
update user set password=password('root123') where user='root';
flush privilege;
低于mysql5.7版本需要 进行安全加固:
delete from user where user!='root' or host!='localhost';
关闭mysql: cd /usr/local/mysql/bin
./mysqladmin -uroot -proot123 shutdown
2.5查询命令:
show databases;
3.安装mysql5.7
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
cd /usr/local/
tar -zxvf mysql5.7.14-linux-glibc2.5-x86_64.tar.gz
ln -s mysql5.7.14-linux-glibc2.5-x86_64 mysql
mkdir -p /data/mysql
vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="\u@db \R:\m\s [\d]>"
no-quto-rehash
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql
socket = /tmp/mysql.sock
character-set-server = utf8mb4
skip_name-resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors =1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instance = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
query_cache_size = 0
query_cache_type = 0
interactive_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /data/mysql/slow.log
log-error = /data/mysql/error.log
long_query_time = 0.5
server-id = 3306100
log-bin = /data/mysql/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consisitency = 1
log_slave_updates
binlog_format = row
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
innodb_status_file = 1
innodb_status_output = 0
innodb_status_output_locks = 0
#performance_schema
performace_schema = 1
performance_schema_instrument = '%=on'
#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
[mysqldump]
quick
max_allowed_packet = 32M
chown mysql:mysql -R /usr/local/mysql
chown mysql:mysql -R /data/mysql
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/loca/mysql --datadir=/data/mysql/ --user=mysql -initialize
#--initialize会生成一个临时数据库密码记录在log-error里,
#--initialize-insecure代表无密码进入,建议使用生成初始化密码
cd /usr/loca/mysql/bin
./mysqld_safe --defaults-file=/etc/my.cnf &
cat /data/mysql/error.log |grep password
设置mysql密码:
mysql -uroot -p生成的密码
set PASSWORD = 'root123'
alter user 'root'@'localhost' PASSWORD EXPIRE NEVER;
flush privileges;
3.1mysqlroot密码丢失:
ps -ef |grep mysql
kill -9 pid
cd /usr/local/mysql/bin
./mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-table &
mysql
use mysql;
update user set authentication_string=password('root') where user='root'
重启数据库即可
./mysqld_safe --defaults-file=/etc/my.cnf &
mysql -uroot -p密码
3.2mysql数据库连接方式
TCP/IP方式(client和server不在同一台上) :
mysql -u username -p password -P port -h IP
Socket方式连接:
mysql -u username -p password -S /tmp/mysql.sock
连接工具有:sqlyog,navicat
3.3用户权限管理:
select user,host ,password from user;
5.7mysql查询:
use mysql
select user,host,authentication_string from user;
专库专用账号进行管理:
创建用户:
create user 用户名@主机ip identified by 'passwork';
主机IP避免使用%,可分配ip地址段
例 :给erp数据库创建只读账号及读写账号:
create user 'erp_read'@'192.168.56.%' identified by 'erp123';
grant select on erp.* to 'erp_read'@'192.168.56.%' identified by 'erp123';
flush privileges;
create user 'erp_user'@'192.168.55.%' identified by 'erp123';
grant select,insert,update,delete on erp.* to 'erp_user'@'192.168.56.%' identified by 'erp123'
flush rivileges;