暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MYSQL安装笔记

叶同学专栏 2021-04-16
251

mysql常用的存储引擎

InnoDB


MyISAM


MEMORY


ARCHIVE


查看存储引擎

mysql> SHOW VARIABLES LIKE 'default_storage_engine%';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)

复制

修改存储引擎

修改配置文件my.cnf
,修改或添加参数

default-storage-engine=MyISAM

复制

安装过程

检查是否已安装过mariadb和mysql,并删除

rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64 mariadb-devel-5.5.64-1.el7.x86_64
rpm -qa | grep mysql
...

复制

创建mysql用户(自动创建同名组)

useradd mysql
passwd mysql

复制

创建数据目录

mkdir /data/mysqldata 
chown -R mysql:mysql /data/mysqldata 

复制

解压安装包,初始化数据库

cd /usr/local
tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.7.24-linux-glibc2.12-x86_64 mysql
cd mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata --basedir=/usr/local/mysql

复制

初始化报错

[root@sdw3]/usr/local/mysql# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata
2020-04-03T07:03:13.401938Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-03T07:03:13.407889Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2020-04-03T07:03:13.407920Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2020-04-03T07:03:13.407925Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2020-04-03T07:03:13.407931Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2020-04-03T07:03:13.407936Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2020-04-03T07:03:13.407941Z 0 [ERROR] Failed to initialize builtin plugins.
2020-04-03T07:03:13.407943Z 0 [ERROR] Aborting

复制

虚拟机设置的内存太小点(2G),再创建1G的swap交换内存空间

dd if=/dev/zero of=/data/swapfile bs=1024 count=1024000
mkswap /data/swapfile
swapon /data/swapfile
chmod 600 /data/swapfile

[root@sdw3]/usr/local/mysql# free -m
              total        used        free      shared  buff/cache   available
Mem:           1821         179         129          68        1513        1358
Swap:           999           0         999

复制

使swap持久

# vi /etc/fstab  
/data/swapfile swap swap default 0 0

复制

初始化日志,生成root的默认密码是5FAt5Z-&3soE

[root@sdw3]/usr/local/mysql# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata --basedir=/usr/local/mysql
2020-04-03T07:07:51.525218Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-03T07:07:52.561018Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-03T07:07:52.730529Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-03T07:07:52.829726Z 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: d5e56ea8-7579-11ea-8e87-000c29543e8f.
2020-04-03T07:07:52.849359Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-03T07:07:52.850620Z 1 [Note] A temporary password is generated for root@localhost: 5FAt5Z-&3soE

复制

启动mysql

mysql.server
启动服务有默认路径,可修改参数basedir
datadir
指定自己的目录,这里我做软链接指定datadir
的目录,basedir
默认是/usr/local/mysql

ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /data/mysqldata /usr/local/mysql/data 
# service mysql start
Starting MySQL. SUCCESS! 

复制

使用密码登陆数据库

ln -s /usr/local/mysql/bin/mysql /usr/bin
# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -u root -p
Enter password:
mysql> 

复制

第一次登陆需要修改密码

mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

复制

或者使用mysqladmin修改密码

/usr/local/mysql/bin/mysqladmin -u root -p123456 password '1234'

复制

重新登陆,并开放root用户远程登陆权限

# mysql -u root -p123456
mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> update user set host = '%' where user = 'root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

复制

添加配置文件my.cnf
,简单的修改服务端口,这配置文件默认优先从basedir
读取,然后再到/etc/my.cnf

[root@sdw3]/usr/local/mysql# service mysql restart  
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@sdw3]/usr/local/mysql# netstat -lntp|grep 3306
tcp6       0      0 :::13306                :::*                    LISTEN      20546/mysqld        
[root@sdw3]/usr/local/mysql# netstat -lntp|grep 3305

[root@sdw3]/usr/local/mysql# cat my.cnf 
[mysqld]
port = 13306
[root@sdw3]/usr/local/mysql# cat /etc/my.cnf
[mysqld]
port = 13305

复制

切换到mysql用户,添加用户下的默认配置

# vim ~/.my.cnf 
[client]
user=root
password=123456
port = 13306

复制

使用默认配置登陆

[mysql@sdw3 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

复制

设置开机启动

cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
chkconfig --list

复制

InnoDB主要参数设置

  • innodb_buffer_pool_size

为了提升写性能,可以把要写的数据先在缓冲区( buffer )里合并,然后再发送给下一级存储。这样做可提高 I/O 操作的效
率。InnoDB Buffer Pool 就是 InnoDB 用来缓存它的数据和索引的内存缓冲区,可由 innodb_buffer_pool_size 设置其大小。理论上,将
这个值设置得越高,访问数据需要的磁盘 I/O 就越少。常见的做法是让这个值大于热点数据,这样可以获得比较好的性能。如
果不清楚环境的数据量和访问模式,建议将其设置为机器物理内存大小的 70%~80% 。

  • innodb_log_file_size

日志组里每个日志文件的大小。在 32 位计算机上日志文件的合并大小必须小于 4GB ,默认大小是 5MB ,在生产环境下,这
个值太小了。官方文档推荐的值为从 1MB 到 1/N 的缓冲池大小,其中 N 是日志组里日志文件的数目(由 innodb_log_files_in_group 变
量来确定,一般默认为 2 )。值越大,在缓冲池中需要检查点刷新的行为就越少,因此也越节约磁盘 I/O ,但更大的日志文件也
意味着在崩溃时恢复得更慢。建议将日志文件的大小设置为 256MB 或更大,这样可以满足一般情况下的需要。

  • innodb_flush_log_at_trx_commit

这个选项的默认值是 1 。当设置为 2 时,在每个事务提交时,日志缓冲被写到文件中,但不对日志文件做刷新到磁盘的操
作。对日志文件的刷新每秒才发生一次。所以,理论上,操作系统崩溃或掉电只会丢失最后一秒的事务。建议设置为2

  • sync_binlog

如果是 autocommit 模式,那么每执行一个语句就会向二进制日志写入一次,否则每个事务写入一次。如果 sync_binlog 的值为
正,那么每当 sync_binlog 参数设定的语句或事务数被写入二进制日志后, MySQL 服务器就会将它的二进制日志同步到硬盘上。
默认值是 0 ,不与硬盘同步。值为 1 是最安全的选择,因为崩溃时,你最多丢掉二进制日志中的一个语句或事务。但是,这也是
最慢的选择,成本昂贵。

查看参数值命令:

show global variables;
show global variables like '%sync_binlog%';

复制


文章转载自叶同学专栏,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论