三、mysql的基本管理
1、数据库关闭启动
数据库启动流程:
关闭:
[root@centos6-kvm3 support-files]# service mysql stop
[root@centos6-kvm3 support-files]# mysqladmin -uroot -poldboy123 shutdown
启动:
[root@centos6-kvm3 support-files]# service mysqld start
[root@centos6-kvm3 support-files]# /application/mysql/bin/mysqld_safe &
野蛮关闭数据库方式:
kill -9 ?
第三种为利用系统进程管理命令关闭MySQL。
kill pid#<==这里的pid为数据库服务对应的进程号。
killall mysqld#<==这里的mysqld是数据库服务对应的进程名字。
pkill mysqld #<==这里的mysqld是数据库服务对应的进程名字。
启动报错案例:
[root@centos6-kvm3 data]# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/application/mysql-5.6.36/data/centos6-kvm3.pid).
查看错误日志:
[root@centos6-kvm3 data]# pwd
/application/mysql/data
[root@centos6-kvm3 data]# cat centos6-kvm3.err
查看报错部分:
2020-03-19 00:07:20 23029 [ERROR] /application/mysql-5.6.36/bin/mysqld: Can't find file: './mysql/user.frm' (errno: 13 - Permission denied)
[root@centos6-kvm3 data]# ll
drwx------ 2 root root 4096 Mar 18 21:32 mysql
[root@centos6-kvm3 data]# chown -R mysql.mysql mysql
编译启动:
[root@centos6-kvm3 data]# mysqld_safe --socket=/tmp/mysql.sock --port=3307 &
[root@centos6-kvm3 data]# mysql -uroot -poldboy123 -S /tmp/mysql.sock
[root@centos6-kvm3 data]# ps -ef | grep mysql
mysql启动参数设置:
1、预编译时候设置参数,参数会硬编码到程序中。
2、命令行方式设定启动参数。
3、初始化的配置文件,/etc/my.cnf
启动优先级:2>3>1
影响到什么?
①影响数据库的启动
[mysqld]
[mysqld_safe]
[server]
②影响到数据库的链接
[mysql]
[mysqladmin]
[mysqldump]
[client]
2、mysql配置文件
配置文件案例:
[root@centos6-kvm3 data]# vim /etc/my.cnf
[mysqld]
basedir=/application/mysql
datadir/application/mysql/data
socket=/application/mysql/tmp/mysql.sock
port=3306
server_id=10
log-error=/var/log/mysql.log #错误日志
log-bin=/application/mysql/data/mysql-bin
binlog_format=row
skip_name_resolve #跳过域名解析
[mysql]
#客户端
socket=/application/mysql/tmp/mysql.sock
[root@centos6-kvm3 data]# service mysqld start
Starting MySQL. SUCCESS!
[root@centos6-kvm3 data]# ps -ef | grep mysql
自定义配置文件启动:
[root@centos6-kvm3 ~]# cat /tmp/aa.txt
[mysqld]
basedir=/application/mysql
datadir=/application/mysql/data
socket=/application/mysql/tmp/mysql.sock
port=3309
server_id=11
log-error=/var/log/mysql.log
log-bin=/application/mysql/data/mysql-bin
binlog_format=row
skip_name_resolve
[root@centos6-kvm3 data]# mysqld_safe --defaults-file=/tmp/aa.txt
[root@centos6-kvm3 ~]# ps -ef | grep mysql
3、配置mysql多实例
思路:
1、启动多个mysqld进程
2、规划多套数据
3、规划多个端口
4、规划多套日志路径
多实例配置
1、创建多套目录
mkdir -p /data/330{7,8,9}
2、准备多套配置文件
vi /data/3307/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3307
server-id=3307
port=3307
log-bin=/data/3307/mysql-bin
socket=/data/3307/mysql.sock
log-error=/data/3307/mysql.log
vi /data/3308/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3308
server-id=3308
port=3308
log-bin=/data/3308/mysql-bin
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
vi /data/3309/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3309
server-id=3309
port=3309
log-bin=/data/3309/mysql-bin
socket=/data/3309/mysql.sock
log-error=/data/3309/mysql.log
3、初始化多套数据
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307 --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3308 --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3309 --user=mysql
4、启动多个实例
mysqld_safe --defaults-file=/data/3307/my.cnf &
mysqld_safe --defaults-file=/data/3308/my.cnf &
mysqld_safe --defaults-file=/data/3309/my.cnf &
5、查看启动端口
netstat -lnp |grep 330
6、分别连接测试
mysql -S /data/3307/mysql.sock -e "show variables like 'server_id';"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id';"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id';"
如果出现闪退,检查是否端口已经被使用:需要关闭在用的端口
mysqladmin -uroot -poldboy123 -S /tmp/mysql.sock shutdown