该笔记是Linux上第一次安装mysql
CentOS版本:7.0 64位
MySQL安装包:mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
没有的小伙伴可以从我这里下载 密码:gwbm
mysql安装位置:/opt/mysql
数据库文件数据位置:/opt/mysql/data
1,上传文件,然后解压到/opt目录下
tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /opt/
2,更改文件夹名字
mv /opt/mysql-5.7.21-linux-glibc2.12-x86_64 /opt/mysql
3,在进入/opt/mysql/目录下并创建data文件夹
cd /opt/mysql/
mkdir data
4,创建mysql用户组和mysql用户
groupadd mysql
useradd -r -g mysql mysql
5,关联myql用户到mysql用户组中
chown -R mysql:mysql /opt/mysql/
chown -R mysql /opt/mysql/
6,更改mysql安装文件夹mysql/的权限
hmod -R 755 /opt/mysql/
7,初始化mysql
cd /opt/mysql/bin/
./mysqld --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data --initialize
在执行上面命令时会出现临时密码
[Note] A temporary password is generated for root@localhost: JasDenA<)j
我这里是不使用临时密码JasDenA<)j登录的
8,修改mysql配置文件
vi /opt/mysql/support-files/mysql.server
一般是第66到第73行左右,修改内容为:
basedir=/opt/mysql
bindir=/opt/mysql/bin
if test -z "$datadir"
then
datadir=/opt/mysql/data
fi
sbindir=/opt/mysql/bin
libexecdir=/opt/mysql/bin
这里使用的是我自己的路径,你也可以改成你自己的
第266行也需要更改,在mysqld_safe后面加上user=root内容为:
$bindir/mysqld_safe --user=root --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
保存并退出
:wq
9,将文件复制到/etc/init.d/目录下
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
赋给权限
chmod 755 /etc/init.d/mysqld
10,修改my.cnf文件
vi /etc/my.cnf
具体内容如下
[client]
no-beep
socket =/opt/mysql/mysql.sock
port=3306
[mysql]
default-character-set=utf8
[mysqld]
datadir=/opt/mysql/data
socket = /opt/mysql/mysql.sock
basedir=/opt/mysql
symbolic-links=0
port=3306
skip-grant-tables
character-set-server=utf8
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
#include all files from the config directory
#
!includedir /etc/my.cnf.d
好了,下面的内容你也可以加上,加在mysqld下面
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true
max_connections=2000
query_cache_size=0
table_open_cache=2000
tmp_table_size=246M
thread_cache_size=300
#限定用于每个数据库线程的栈大小。默认设置足以满足大多数应用
thread_stack = 192k
key_buffer_size=512M
read_buffer_size=4M
read_rnd_buffer_size=32M
innodb_data_home_dir = /data/mysql
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16M
innodb_buffer_pool_size=256M
innodb_log_file_size=128M
innodb_thread_concurrency=128
innodb_autoextend_increment=1000
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
back_log=80
flush_time=0
join_buffer_size=128M
max_allowed_packet=1024M
max_connect_errors=2000
open_files_limit=4161
query_cache_type=0
sort_buffer_size=32M
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
#批量插入数据缓存大小,可以有效提高插入效率,默认为8M
bulk_insert_buffer_size = 64M
interactive_timeout = 120
wait_timeout = 120
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
11,进入/var/log/文件夹下,并创建文件夹mariadb
cd /var/log/
mkdir mariadb
12,进入mariadb文件下,并创建文件
cd mariadb
touch mariadb.log
13,给mariadb文件赋mysql的所有权限
chown -R mysql:mysql /var/log/mariadb*
chown -R mysql /var/log/mariadb*
14,启动mysql
/etc/init.d/mysqld start
如果不成功就将/opt/mysql/bin/mysqld_safe文件里面的/usr/local/mysql全部改为/opt/mysql
15,登录mysql
/opt/mysql/bin/mysql -uroot -p
直接回车不用输入密码,依次输入下面的内容:
flush privileges;
grant all privileges on *.* to root@'%' identified by 'root';
use mysql;
alter user 'root'@'localhost' identified by 'root';
flush privileges;
quit
可以了,密码为root了
16,然后修改配置文件
vi /etc/my.cnf
将skip-grant-tables进行注释
#skip-grant-tables
将服务重启
service mysqld stop
service mysqld start
17,重新登录mysql
/opt/program/mysql/bin/mysql -uroot -p
这次密码不正确就进不去了
18,设置mysql开机自启
添加服务
chkconfig --add mysqld
显示服务列表
chkconfig --list
如果看到mysqld的服务,并且3,4,5都是on的话则成功,如果是off,则键入
chkconfig --level 345 mysqld on
重启电脑,进行验证
reboot