导航

涂抹mysql笔记-管理mysql服务

Posted on 2017-04-10 11:49  datalife  阅读(559)  评论(4编辑  收藏  举报

-DSYSCONFDIR=/mysql/conf \ 所以在conf下建立my.cnf文件

vi my.cnf

[client]
port=3306
socket=/mysql/conf/mysql.sock

#The MySQL server
[mysqld]
port=3306
user=mysql
socket=/mysql/conf/mysql.sock
pid-file=/mysql/conf/mysql.pid
basedir=/mysql
datadir=/mysql/data
tmpdir=/mysql/tmp
open_files_limit=10240
explicit_defaults_for_timestamp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#Buffer
max_allowed_packet=256M
max_heap_table_size=256M
net_buffer_length=8k
sort_buffer_size=2M
join_buffer_size=4M
read_buffer_size=2M
read_rnd_buffer_size=16M

#log
log-bin=/mysql/binlog/mysql-bin
binlog_cache_size=32M
max_binlog_cache_size=512M
max_binlog_size=512M
binlog_format=mixed
log_output=FILE
log-error=/mysql/logs/mysql-error.log
slow_query_log=1
slow_query_log_file=/mysql/logs/slow_query.log
general_log=0
general_log_file=/mysql/logs/general_query.log
expire-logs-days=14

#InnoDB
innodb_data_file_path=ibdata1:2048M:autoextend
innodb_log_file_size=256M
innodb_log_files_in_group=3
innodb_buffer_pool_size=1024M

[mysql]
no-auto-rehash
prompt=(\u@\h) [\d]>\
default-character-set=gbk

初始化mysql数据库:
$ /mysql/scripts/mysql_install_db --datadir=/mysql/data --basedir=/mysql

启动mysql数据库

support-files/mysql.server脚本启动mysql不推荐,推荐使用mysqld_safe启动:
$ mysqld_safe --defaults-file=/mysql/conf/my.cnf &
netstat -lnt |grep 3306
ps -ef |grep mysql 查看mysql端口和进程

配置mysql
mysql5.6默认root用户不设密码的直接敲入mysql登录。
增强安全性:
select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | linux01 |
| root | linux01 |
| | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.01 sec)
6个用户,两个用户没有用户名。所以直接可通过mysql登录。
下面保留一条root用户其他用户全部删除。

delete from mysql.user where (user,host) not in (select 'root','localhost');
修改root用户名
update mysql.user set user='system',password=password('oralinux');
(root@localhost) [(none)]>select * from mysql.db \G

(root@localhost) [(none)]>truncate table mysql.db;

(root@localhost) [(none)]>flush privileges;

root@localhost) [(none)]>exit
Bye
[mysql@linux01 mysql]$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[mysql@linux01 mysql]$ mysql -uroot -p'oralinux'
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[mysql@linux01 mysql]$ mysql -usystem -p'oralinux'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.34-log JASON

Copyright (c) 2000, 2016, 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.

(system@localhost) [(none)]>

创建管理脚本,创建中间定义文件,目的是提高脚本的复用性:
vi /mysql/scripts/mysql_env.ini
#set env
MYSQL_USER=system
MYSQL_PASS='oralinux'

#check parameter
if [ $# -ne 1 ]
then
HOST_PORT=3306
else
HOST_PORT=$1
fi
$ chmod 600 /mysql/scripts/mysql_env.ini
创建mysql_db_startup.sh脚本,用于启动mysql服务:
#!/bin/sh
# Created by jason
source /mysql/scripts/mysql_env.ini
echo "Startup MySQL Service:localhost_"${HOST_PORT}
/mysql/bin/mysqld_safe --defaults-file=/mysql/conf/my.cnf &

创建mysql_db_shutdown.sh脚本,用于关闭mysql服务:

$ vi /mysql/scripts/mysql_db_shutdown.sh
#!/bin/sh
#Created by jason
source /mysql/scripts/mysql_env.ini
echo "Shutdown MySQL Service:localhost_"${HOST_PORT}
/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S /mysql/conf/mysql.sock shutdown
创建sqlplus.sh脚本用于快速登录:
$vi /mysql/scripts/sqlplus.sh
#!/bin/sh
#Created by jason
source /mysql/scripts/mysql_env.ini
echo "Login MySQL Service:localhost_"${HOST_PORT}
/mysql/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /mysql/conf/mysql.sock $2
chmod +x /mysql/scripts/sqlplus.sh
编辑mysql用户变量,将上述路径加入到PATH中
echo "export PATH=/mysql/scripts:\$PATH" >> ~/.bash_profile
source ~/.bash_profile
设置开机启动:root下
# vi /etc/rc.localhost
# autostart mysql ,added by jason at 2017-03-31
sudo -i -u mysql /mysql/scripts/mysql_db_startup.sh 3306 > /mysql/logs/mysql_db_startup.log 2>&1
执行sudo命令遇到下列错误:
sudo:sorry,you must have a tty to run sudo
vi /etc/sudoers 注释掉56行
#Defaults requiretty