mysql简介

MYSQL介绍

  MySQL一般特制完整的MySQLRDBMS,是一个开源的关系型数据库管理系统(Relational Database Management System),现在属于Oracle公司。随着MySQL功能的不断完善,性能不断提高,又有开源免费的优势,越来越多的企业选择使用MySQL,而放弃商用收费的Oracle,目前最新版为:5.8

MYSQL体系

mysql结构体系::分两层:mysql server层、存储引擎层
   mysql server层: 分两层:连接层、SQL层
        连接层: 
            通信协议:兼容性
            线程处理: 连接线程分配,一个线程对应一个逻辑CPU,并会在多个逻辑CPU之间进行切换
            用户名密码:用户密码判断,以及授权信息
        SQL层:权限判断、查询缓存、解析器、预处理、查询优化器、缓存和执行计划
    存储引擎层: MYisam、innodb、NDB、.....

mysql简介

1、MYSQL安装前配置

1.1、swap分区设置

  swappiness对swap分区有很大的影响性,它这有两个极限值 0:最大限度使用物理内存, 100:积极使用swap分区,这里建议不用swap分区或分配4G内存空间
cat /proc/sys/vm/swappiness

1.2、操作系统的限制

ulimit -a查看
# 最大打开文件个数,如果文件打开句柄超过1024就会报错
    open files                      (-n) 1024
# 使用最大的进程数,最大进程7279
    max user processes              (-u) 7279
可以编辑 /etc/security/limits.conf加入限制的内容
*    soft       noproc   65535
*    head       noproc   65535
*    soft       nofile   65535
*    head       nofile   65535
\# 修改完之后重启系统才会生效

1.3、mysql读取配置文件顺序

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cn

1.4、mysql启动选项

1、 defaults-file: 使用指定的配置文件
2、 defaults-extra-file: 除了读取默认的配置文件,还读取额外的配置文件
3、 no-defaults: 忽略所有的配置文件
如果一次性指定多个配置文件,则以最后一次读取的为准 

1.5、my.cnf配置

my.cnf 大部分来自腾讯MYSQL的优化配置

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

auto_increment_increment=1
auto_increment_offset=1
back_log=210
character_set_server=UTF8
concurrent_insert=AUTO
connect_timeout=10
default_week_format=0
div_precision_increment=4
event_scheduler=OFF
group_concat_max_len=1024
innodb_adaptive_hash_index=ON
innodb_autoinc_lock_mode=1
innodb_concurrency_tickets=5000
innodb_flush_log_at_trx_commit=2
innodb_ft_max_token_size=84
innodb_ft_min_token_size=3
innodb_large_prefix=OFF
innodb_lock_wait_timeout=7200
innodb_max_dirty_pages_pct=75
innodb_old_blocks_pct=37
innodb_old_blocks_time=1000
innodb_open_files=1024
innodb_print_all_deadlocks=OFF
innodb_purge_batch_size=300
innodb_purge_threads=1
innodb_read_ahead_threshold=56
innodb_read_io_threads=12
innodb_rollback_on_timeout=OFF
innodb_stats_on_metadata=OFF
innodb_strict_mode=ON
innodb_table_locks=ON
innodb_thread_concurrency=0
innodb_thread_sleep_delay=10000
innodb_write_io_threads=12
interactive_timeout=3600
lock_wait_timeout=31536000
log_queries_not_using_indexes=OFF
long_query_time=10.000000
low_priority_updates=OFF
lower_case_table_names=0
max_allowed_packet=1073741824
max_connect_errors=999999999
max_connections=800
max_length_for_sort_data=1024
max_prepared_stmt_count=16382
max_user_connections=0
net_read_timeout=30
net_retry_count=10
net_write_timeout=60
open_files_limit=102400
query_alloc_block_size=8192
query_cache_limit=1048576
query_cache_size=0
query_cache_type=OFF
query_prealloc_size=8192
slow_launch_time=2
sql_mode=NO_ENGINE_SUBSTITUTION
sync_binlog=0
table_definition_cache=768
table_open_cache=512
tmp_table_size=209715200
wait_timeout=3600
[mysqld_safe]
log-error=/data/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid

2、安装

mysql 5.6可以直接用mysql_install_db安装这里就不在讲解

2.1、MYSQL5.7安装

  安装前需了解:5.7安装初始化已经废弃了mysql_install_db这个命令

[root@do3 local]# cd /usr/local/
# 创建mysql用户
[root@do3 local]# useradd -u 3010 mysql -s /sbin/nologin
[root@do3 local]# mkdir -p /data/mysql
# 我们直接使用mysql二进制文件安装
[root@do3 local]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 
# 使用软链的方式,方便下次升级
[root@do3 local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64"
# 设置权限
[root@do3 local]# chown mysql.mysql mysql-5.7.22-linux-glibc2.12-x86_64 -R
[root@do3 local]# chown mysql.mysql /data/mysql/ -R
[root@do3 local]# cd mysql/bin
# 增加环境变量
[root@do3 mysql]# echo "export PATH=${PWD}:\$PATH" > /etc/profile.d/mysql.sh
[root@do3 mysql]# source !$
# 这里很重要,初始化mysql安装
[root@do3 mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/
2018-04-27T12:05:57.483766Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-27T12:05:57.483806Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2018-04-27T12:05:57.486165Z 0 [Warning] InnoDB: Using innodb_large_prefix is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
2018-04-27T12:05:59.175629Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-04-27T12:05:59.494640Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-04-27T12:05:59.546806Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 59289cc1-4a13-11e8-9261-0050568a0453.
2018-04-27T12:05:59.579060Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-04-27T12:05:59.579540Z 1 [Note] A temporary password is generated for root@localhost: SdqwoGENy8+k

# 增加一个systemv风格的启动脚本
[root@do3 bin]# cd ..
[root@do3 mysql]# vim support-files/mysql.server
修改这两行
    basedir=/usr/local/mysql
    datadir=/data/mysql

[root@do3 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@do3 mysql]# chmod +x !$
[root@do3 mysql]# mkdir /var/lib/mysql
[root@do3 mysql]# chown mysql.mysql /var/lib/mysql/ -R

# 启动服务
[root@do3 mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/mariadb.log'.
 SUCCESS! 

2.2、MYSQL初始密码设置

# mysql 初始密码会在init的时候就直接设置 也可以初始化之后查看error.log文件
[root@do3 mysql]# mysql -uroot -p
Enter password:   SdqwoGENy8+k
# 进入之后第一次是需要你修改默认密码的,强制性要求
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter  user 'root'@'localhost' identified by 'xiong123';

# 丢失root密码设置
配置文件中新增加一行  skip-grant-tables=1
或者,直接跳过权限,这样也能直接修改密码
[root@do3 mysql]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &

有可能会提示这样的错误
    ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    解决: mysql> flush privileges; 
           mysql> alter user 'root'@'localhost' identified by 'xiong123';

猜你喜欢

转载自blog.51cto.com/xiong51/2114458