[root@lbg mysql3306]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@lbg mysql3306]# uname -r 3.10.0-693.el7.x86_64 |
[root@master ~]# hostname lbg
[root@master ~]# hostnamectl set-hostname lbg ---vim /etc/sysconfig/network [root@lbg /]# systemctl stop firewalld.service [root@lbg /]# systemctl disable firewalld.service [root@lbg /]# setenforce 0 [root@lbg /]# vim /etc/selinux/config ---SELINUX=disabled [root@lbg /]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens33 DEVICE=ens33 ONBOOT=yes IPADDR0="192.168.88.8" PREFIX0="24" [root@lbg /]# ifconfig ens33 192.168.88.8/24 up [root@lbg /]# systemctl restart network |
2.创建目录放置安装包并解压
[root@lbg ~]# mkdir /soft
[root@lbg soft]# ls my.cnf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz [root@lbg soft]# tar xvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz [root@lbg soft]# mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql ---不要新建/usr/local/mysql 说明:如果没有特殊情况,一般把二进制的安装包解放置到/usr/local/mysql下面,因为其为mysql默认安装目录。 |
3.创建用户和数据目录,修改权限,添加环境变量
[root@lbg soft]# useradd mysql
[root@lbg soft]# mkdir -p /home/mysql3306/mysql3306 [root@lbg soft]# mkdir -p /home/mysql3306/logs [root@lbg soft]# chown -R mysql.mysql /home/mysql3306 [root@lbg soft]# chown -R mysql.mysql /usr/local/mysql [root@lbg logs]# vim /etc/profile export MYSQL_HOME=/usr/local/mysql export PATH=$PATH:$MYSQL_HOME/bin |
4.修改配置参数文件
[root@lbg soft]# vim my.cnf
[root@lbg soft]# mv -f /soft/my.cnf /etc/my.cnf 注意:socket文件的具体目录不用创建(/tmp/mysql3306.sock).当启动mysql时自动创建. |
5.mysql5.6初始化
先安装依赖包:
[root@lbg soft]# yum -y install perl-Data-Dumper mysql5.6初始化: [root@lbg soft]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --user=mysql 说明:初始化若出现两个OK即初始化成功.可查看/home/mysql3306/mysql3306里文件。 初始化目的:mysql有表结构 系统文件 数据字典等初始信息,需要进行初始化,初始化只需要在第一次使用的时候进行.(用root用户运行,指定user为mysql即可.) 初始化的目录里不能有文件,初始化时defaults-file最后要写上,否则会默认读/etc/my.cnf 文件,若配置文件名不为my.cnf,则可能出现问题。 |
执行mysql_install_db脚本后,初始化过程中,进行了什么操作?
1.检查相关传入参数
2.检查相关的数据目录是否存在 3.创建相关的系统表结构 4.完成并且打印提示 |
6.mysql5.6启动
[root@lbg mysql3306]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql & |
启动流程:
MySQL程序首先检查环境变量,然后检查配置文件,最后检查终端的选项,说明终端指定选项优先级最高. |
说明:注意启动用/usr/local/mysql/bin/mysqld_safe,而非使用/usr/local/mysql/bin/mysqld,但实际上/usr/local/mysql/bin/mysqld_safe是调用/usr/local/mysql/bin/mysqld文件的。
mysqld_safe在启动的时候的作用:
1.检查传入参数
2.检查相关路径 4.对配置文件进行检查,并且对配置文件进行解析 5.启动mysqld 6.启动时判断进程号是否存在,如果存在报错,不存在删除进程文件(如果有的话),如果无法删除,报错。 7.如果启动没有给参数会使用默认参数 8.启动过程中会检查相关表结构是否损坏,如果有异常关闭而造成了表结构的损坏,使用mysqld_safe无法启动,这个时候可以使用mysqld启动,然后对相应的表结构进行修复,或者重建。 9.服务器异常关闭,会自动restart(守护进程) |
选用mysqld_safe的好处:
1.mysqld_safe增加了安全特性
2.防止启动多个同一端口的mysql实例 3.检查表结构是否完整 2.mysqld_safe 可以使数据库restart,打印更加全面的日志信息. |
7.mysql登录
[root@lbg mysql3306]#
/usr/local/mysql/bin/mysql 说明:在本地使用,默认用户是root,默认端口是3306,实际是通过读取了参数文件my.cnf中[client]模块下的socket参数指定的socket文件。 |
mysql登录方式说明:
mysql客户端远程链接mysql
server的过程中,其实是使用tcpip协议,发送了指定的协议数据包,所有的数据库远程链接过程中,基本都是采⽤这种方法。⽽本地采⽤socket链接就是使⽤物理⽂件,采⽤Unix
Socket协议,效率最高。查看socket文件位置:mysql> show variables like 'socket';
在使用正确的MySQL链接进入数据库后,⾸先会进行权限的对比,对比mysql.user表中的数据,看链接用户是否有链接mysql.server的权限。 |
8.设置登录权限和密码
mysql> select user,host,password from
mysql.user;
---第1次密码为空,所以可以无密码登录。
+------+-----------+----------+
| user | host
| password |
+------+-----------+----------+
| root | localhost |
|
| root | lbg
|
|
| root | 127.0.0.1 |
|
| root | ::1
|
|
|
| localhost
|
|
|
| lbg
|
|
+------+-----------+----------+
6 rows in set (0.00 sec)
删除其他用户,只保留root/localhost即可.
delete from mysql.user where user=''; delete from mysql.user where host='::1'; delete from mysql.user where host='lbg'; delete from mysql.user where host='127.0.0.1'; |
mysql赋予权限,授权后用户就有了密码.登录就需密码:
grant all on *.* to root@'localhost' identified by 'root' with
grant option; grant all on *.* to root@'%' identified by 'root' with grant option; flush privileges; ---刷新系统权限相关表。说明:*.* 指所有库的所有对象, *数据库 *表 如: test1.* test1.t1 root@'localhost' 指root用户名在localhost网段。root@'%'指代所有网段,例如:root@'192.168.2.%' identified by 'root'; 密码是root 。 with grant option 给该用户复权权限。 |
本地登录数据库:
[root@lbg ~]# /usr/local/mysql/bin/mysql
-uroot -proot -S /tmp/mysql3306.sock
--默认端口号 [root@lbg ~]# /usr/local/mysql/bin/mysql -uroot -proot ---读参数文件中的socket文件 [root@lbg ~]# /usr/local/mysql/bin/mysql -uroot -p ---不明文写密码 [root@lbg ~]# mysql -p ---默认root用户登录 |
使用远程连接数据库:
[root@lbg ~]# /usr/local/mysql/bin/mysql -uroot -proot -h192.168.88.8 -P3306 |
9.关闭数据库
正常关闭方式:
[root@lbg
~]# /usr/local/mysql/bin/mysqladmin
-uroot -proot -S /tmp/mysql3306.sock
shutdown 说明:mysqladmin还可用于修改密码。 ⽆密码修改: mysqladmin -u root password 123456 --设置密码为123456 已有密码修改:mysqladmin -u root -p password 123456 ---会提示输入旧密码。 |
[root@lbg ~]# ps -ef |grep mysql
root 2543 2127 0 15:43 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql mysql 3402 2543 22 15:43 pts/1 00:00:01 /usr/local/mysql/bin/mysqld -basedir=/usr/local/mysql --datadir=/home/mysql3306/mysql3306 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/home/mysql3306/logs/mysql-error.log --open-files-limit=65535 --pidfile=/home/mysql3306/mysql.pid --socket=/tmp/mysql3306.sock --port=3306 [root@lbg ~]# kill 3402 2543 ---子进程和父进程都关闭 |
10.删除mysql
pkill mysql
rm -rf /usr/local/mysql rm -rf /home/mysql3306/mysql3306/* rm -rf /home/mysql3306/logs/* |
mysql5.7安装:
mysql5.7安装步骤中与mysql5.6的不同在于初始化,其他大致相同。
[root@lbg soft]# mv mysql-5.7.23-linux-glibc2.12-x86_64
/usr/local/mysql
|
[root@lbg soft]#
/usr/local/mysql/bin/mysqld
--defaults-file=/etc/my.cnf --initialize-insecure
--basedir=/usr/local/mysql --user=mysql 说明: 初始化是使用 /usr/local/mysql/bin/mysqld。 --defaults-file=/etc/my.cnf 一定写到第一个参数 --initialize-insecure 初始化(不会设置初始化密码 5.6一样,密码为空) --initialize 初始化(会设置初始化密码,生成随机密码,在eror日志可以找到) |
[root@lbg mysql3306]# ls
auto.cnf
ibdata1
ib_logfile1
mysql-bin.000001
performance_schema undo001
undo003
ib_buffer_pool ib_logfile0
mysql
mysql-bin.index
sys
undo002
|
[root@lbg soft]# mysqld_safe --user=mysql
&
---启动 [root@lbg soft]# mysql ---登录 |
mysql> select user,host,authentication_string from
mysql.user; --不再是password了。 mysql> delete from mysql.user where user like '%mysql%'; mysql> grant all on *.* to root@'localhost' identified by 'root' with grant option; mysql> grant all on *.* to root@'%' identified by 'root' with grant option; mysql> flush privileges; mysql> show variables like '%version%'; ---查看mysql系统版本 |
密码丢失 ,启动时加上参数:skip-grant-tables:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --user=mysql & 然后登陆: /usr/local/mysql/bin/mysql 再重新给密码,不能用grant all on *.* to root@'localhost' identified by 'root';会报错. mysql> grant all on *.* to root@'localhost' identified by 'root'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 使用update更新密码.用函数password(). update mysql.user set authentication_string=password('root') where user='root'; #重新载入权限表 flush privileges; |
mysql的配置文件:
[client]
port
=
3306
socket
= /tmp/mysql3306.sock
[mysqld]
port
=
3306
socket
= /tmp/mysql3306.sock
datadir
=
/home/mysql3306/mysql3306
#read_only
= on
#--- GLOBAL ---#
lower_case_table_names = 1
log-output
= FILE
log-error
=
/home/mysql3306/logs/mysql-error.log
#general_log
general_log_file
=
/home/mysql3306/logs/mysql.log
pid-file
=
/home/mysql3306/mysql.pid
slow-query-log
= 1
slow_query_log_file
= /home/mysql3306/logs/mysql-slow.log
tmpdir
=
/tmp/
long_query_time
= 2
innodb_force_recovery =
0
#innodb_buffer_pool_dump_at_shutdown = 1
#innodb_buffer_pool_load_at_startup = 1
#--------------#
#thread_concurrency
= 8
thread_cache_size
= 51
table_open_cache
= 16384
open_files_limit
= 65535
table_definition_cache = 16384
sort_buffer_size
= 2M
join_buffer_size
= 2M
read_buffer_size
= 2M
read_rnd_buffer_size =
8M
key_buffer_size
= 32M
bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 64M
tmp_table_size
=
32M
max_heap_table_size
= 16M
query_cache_size
= 32MB
#gtid_mode=on
#log_slave_updates=1
#enforce_gtid_consistency=1
#--- NETWORK ---#
back_log
=
103
max-connections
= 512
max_connect_errors
= 100000
max_allowed_packet
= 32M
interactive_timeout
= 600
wait_timeout
= 600
skip-external-locking
#max_user_connections =
0
external-locking
= FALSE
#skip-name-resolve
#--- REPL ---#
server-id
=
88083306
sync_binlog
= 1
log-bin
= mysql-bin
binlog_format
= row
expire_logs_days
= 10
relay-log
=
relay-log
replicate-ignore-db
= test
log_slave_updates
=1
#skip-slave-start
binlog_cache_size
=4M
max_binlog_cache_size
=8M
max_binlog_size
=1024M
#--- INNODB ---#
default_storage_engine
=
InnoDB
innodb_data_file_path
= ibdata1:1024M:autoextend
innodb_buffer_pool_size
=
800M
innodb_buffer_pool_instances
= 1
innodb_log_files_in_group
= 2
innodb_log_file_size
= 256MB
innodb_log_buffer_size
=
16M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout
= 30
innodb_flush_method
= O_DIRECT
innodb_max_dirty_pages_pct
= 75
innodb_io_capacity
= 200
innodb_thread_concurrency
= 32
innodb_open_files
=
65535
innodb_file_per_table
= 1
transaction_isolation
= REPEATABLE-READ
innodb_locks_unsafe_for_binlog = 0
#innodb_purge_thread
= 4
skip_name_resolve
= 1
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
auto-rehash
# Remove the next comment character if you are not familiar
with SQL
#safe-updates
default_character_set=utf8
[mysqlhotcopy]
interactive-timeout
|
2.mysql5.7配置文件:
[client]
port
= 3306
socket
= /tmp/mysql3306.sock
default_character_set
= utf8mb4
[mysql]
default_character_set
= utf8mb4
[mysqld]
lower_case_table_names
= 1
port
= 3306
basedir
= /usr/local/mysql
datadir
=
/home/mysql3306/mysql3306
socket
= /tmp/mysql3306.sock
pid_file
= /home/mysql3306/mysql3306.pid
tmpdir
= /tmp/
skip_name_resolve
= 1
character_set_server
= utf8mb4
collation_server
=
utf8mb4_unicode_ci
max_connections
= 2000
max_connect_errors
= 10000
interactive_timeout
= 600
wait_timeout
= 600
table_open_cache
=
2048
query_cache_type
= 0
#query_cache_size
= 64M
#query_cache_limit
= 2M
thread_cache_size
= 51
max_allowed_packet
= 16M
tmp_table_size
= 256M
max_tmp_tables
= 128
max_heap_table_size
= 96M
sort_buffer_size
= 4M
read_buffer_size
=
4M
join_buffer_size
= 4M
read_rnd_buffer_size
= 8M
bulk_insert_buffer_size
=
64M
log_error
= /home/mysql3306/logs/mysql-error.log
log_timestamps
= system
slow_query_log
= 1
slow_query_log_file
=
/home/mysql3306/logs/mysql-slow.log
long_query_time
= 1
log_queries_not_using_indexes
= 1
log_throttle_queries_not_using_indexes =10
log_slow_admin_statements
= 1
log_slow_slave_statements
= 1
min_examined_row_limit
=
100
log_queries_not_using_indexes
= 1
log_output
=
FILE
relay_log
= /home/mysql3306/mysql3306/mysql-relay
#binlog
server_id
= 88083306
log_bin
=
/home/mysql3306/mysql3306/mysql-bin
expire_logs_days
= 7
binlog_format
= row
max_binlog_size
= 1024M
max_binlog_cache_size
= 8G
binlog_cache_size
= 4M
sync_binlog
= 1
master_info_repository
=
TABLE
relay_log_info_repository
= TABLE
relay_log_recovery
= ON
log_slave_updates
= 1
#rep
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
#innodb
default_storage_engine
=
InnoDB
innodb_page_size
=
16384
innodb_data_home_dir
= /home/mysql3306/mysql3306/
innodb_data_file_path
= ibdata1:512M:autoextend
innodb_log_group_home_dir
=
/home/mysql3306/mysql3306
innodb_buffer_pool_instances
= 1
innodb_buffer_pool_size
=
600M
innodb_buffer_pool_dump_pct
= 40
innodb_page_cleaners
= 8
innodb_log_file_size
= 256M
innodb_log_files_in_group
= 2
innodb_log_buffer_size
=
32M #default 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout
= 30
innodb_strict_mode
= 1
innodb_print_all_deadlocks
= 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_dump_pct
= 40
innodb_open_files
=
65536
innodb_file_per_table
= 1
innodb_lock_wait_timeout
= 30
innodb_read_io_threads
= 8
innodb_write_io_threads
= 8
innodb_io_capacity
= 200
innodb_flush_log_at_trx_commit = 1
innodb_flush_method
= O_DIRECT
innodb_purge_threads
= 4
innodb_support_xa
= 1
innodb_max_dirty_pages_pct
= 75
transaction_isolation
= READ-COMMITTED
innodb_page_cleaners
= 16
explicit_defaults_for_timestamp = 1
#undo
innodb_undo_directory
= /home/mysql3306/mysql3306/
innodb_undo_logs
=
128
innodb_undo_tablespaces
= 3
innodb_undo_log_truncate
= 1
innodb_max_undo_log_size
= 1000M
innodb_purge_rseg_truncate_frequency = 128
[mysqldump]
max_allowed_packet
= 16M
|