标签(linux): mysql

笔者Q:972581034 架构师群:605799367。有任何疑问可与笔者或加群交流


一、环境

[root@db02 ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[root@db02 ~]# uname -r
2.6.32-642.el6.x86_64
[root@db02 ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
[root@db02 ~]# getenforce 
Disabled

二、安装相关包

yum install ncurses-devel libaio-devel -y
yum install cmake -y

三、安装mysql

useradd -s /sbin/nologin -M mysql
cd /home/oldboy/tools/
ls -l mysql-5.6.34.tar.gz
tar xf mysql-5.6.34.tar.gz
cd mysql-5.6.34
#注意,斜杠后面不能有空格,会导致报错。最好先放到notepad++编辑好再执行
#编译好后查看编译参数可用cmake -LH
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 \
-DMYSQL_DATADIR=/application/mysql-5.6.34/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
#编译
make && make install
ln -s /application/mysql-5.6.34/ /application/mysql
#创建配置文件
\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf

四、初始化数据库,生成初始库

#指定mysql根目录,指定数据文件,指定用户,注意主机名解析。看到两个OK表示初始化成功
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

五、授权

#权限问题会导致mysql启动时无法写入相关文件,导致报错
chown -R mysql.mysql /application/mysql/
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
chkconfig mysqld on
chkconfig --list mysqld
/etc/init.d/mysqld start

六、配置环境变量

echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile
tail -1 /etc/profile
. /etc/profile
echo $PATH

七、设置密码

mysqladmin -u root password 'oldboy123'
mysql -u root -poldboy123
##交互式设置密码方法
mysql_secure_installation

八、安全优化,删除不需要的库和用户

select user,host from mysql.user;
drop user ''@'db02';
drop user ''@'localhost';
drop user 'root'@'db02';
drop user 'root'@'::1';
select user,host from mysql.user;
show databases;
drop database test;
show databases;

到这里mysql的安装已经完成了,下面介绍下密码相关的知识。

修改密码,mysql命令行

UPDATE mysql.user SET password=PASSWORD("123") WHERE user='root' and host='localhost';
flush privileges;

忘记密码的方法

#linux命令行
 mysqld_safe --skip-grant-tables --user=mysql >/dev/null 2>&1 &
/etc/init.d/mysqld stop
mysqld_safe --skip-grant-tables --user=mysql >/dev/null 2>&1 &
mysql
#mysql命令行
UPDATE mysql.user SET password=PASSWORD("123456") WHERE user='root' and host='localhost';

# 5.7版本修改方法
UPDATE mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';

flush privileges;
quit
#linux命令行
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
mysql -uroot -p123