mysql编译安装脚本
yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
useradd -s /sbin/nologin mysql
cd /usr/local/src
wget "https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.47.tar.gz"
tar -zxvf mysql-5.6.47.tar.gz
cd mysql-5.6.47
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOACL_INFILE=1
make && make install
cp support-files/mysql.server /etc/init.d/mysqld #用来启动mysql的脚本
chmod a+x /etc/init.d/mysqld
编译选项说明
- CMACK_INSTALL_PREFIX 指定安装的目录
- MYSQL_DATADIR 指定mysql的数据目录
mysql环境变量配置
echo "export PATH=\$PATH:/usr/local/mysql/bin" >>/etc/profile && source /etc/profile
mysql的启动步骤
- 更新配置
更新mysql配置/etc/my.cnf
cat >/etc/my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0 #监听地址
port=3306 #监听端口
datadir=/data/mysql #数据存放目录
user=mysql #启动用户
skip-name-resolve
long_query_time=2 #和下一句配置结合:查询超过2秒,则记录到日志
slow_query_log_file=/data/mysql/mysql-slow.log
expire_logs_days=2
innodb-file-per-table=1
innodb_flush_log_at_trx_commit=2
log_warnings=1
max_allowed_packet=512M
connect_timeout=60
net_read_timeout=120
[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
EOF
- 数据库初始化
mkdir -pv /data/mysql
chown -R mysql:mysql /usr/local/mysql /data/mysql
yum install -y perl-Module-Install
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/
- 启动数据库
3.1 使用systemctl管理mysql,/usr/lib/systemd/system/mysqld.service
cat >/usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql
TimeoutSec=600
Restart=always
PrivateTmp=false
EOF
3.2 启动 systemctl start mysqld
3.3 验证mysql是否启动
3.3.1 查看进程 ps auxf | grep mysql
3.3.2 查看监听端口 netstat -tulnp | grep mysql
3.3.3 查看日志 cat /data/mysql/mysqld.log
mysql的安全方式
- mysql能限制ip访问、访问需要用户名密码、mysql权限细分
- 默认让127.0.0.1访问,用户名root,不需要密码
- mysql -u root -h 127.0.0.1 -A
mysql加密码
mysqladmin -h 127.0.0.1 -uroot password "zabbixpwd"
mysql -h 127.0.0.1 -uroot -pzabbixpwd
mysql授权某IP登录,指定用户名
grant all privileges on *.* to 'root'@'192.168.3.%' identified by 'zabbixpwd' with grant option;
flush privileges;
网络登录mysql
mysql -h 192.168.3.108 -uroot -pzabbixpwd -A # -A:不预读数据库