1. 检查MySQL是否安装,如果有安装,则移除(rpm –e 名称)
[root@localhost ~]# rpm -qa | grep -i mysql
mysql-libs-xxxxxxxxxx.x86_64
[root@localhost ~]# yum -y remove mysql-libs* --nodeps --force
2. 安装MySQL,并修改配置文件位置
[root@localhost rpm]# rpm -ivh mysql-community-common-5.7.22-1.el6.x86_64.rpm
[root@localhost rpm]# rpm -ivh mysql-community-libs-5.7.22-1.el6.x86_64.rpm
[root@localhost rpm]# rpm -ivh mysql-community-client-5.7.22-1.el6.x86_64.rpm
[root@localhost rpm]# rpm -ivh mysql-community-server-5.7.22-1.el6.x86_64.rpm
3.修改配置/etc/my.cnf文件,修改数据存放路径、mysql.sock路径以及默认编码utf-8.
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
# ###################公共参数#########################################
# basedir = .....
datadir = /home/mysql/data
port = 3306
# server_id = .....
socket =/var/lib/mysql/mysql.sock
log-error=/home/mysql/log/mysqld.log
pid-file=/home/mysql/mysqld.pid
# ###################主要参数#########################################
sql_mode=STRICT_TRANS_TABLES
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
symbolic-links=0
# ###################其它参数#########################################
# 缓存innodb打开的数据. 建议,专用服务器设置:70%, 其它设置: 10%.
innodb_flush_log_at_trx_commit = 0
innodb_buffer_pool_size = 1024M
innodb_log_buffer_size = 8M
innodb_log_file_size = 4G
# 操作DDL/DML/DCL产生的2进制格式log日志,用于主从服务器之间的同步数据
server-id=97
log_bin = master-bin
log_bin_index=master-bin.index
# relay_log=slave-bin
# relay_log_index=slave-bin.index
binlog_do_db=system
# 慢查询日志
slow_query_log=1
long_query_time=10
slow_query_log_file=/home/mysql/log/log-slow-queries.log
# 字符集
character_set_server = utf8
collation_server = utf8_general_ci
# mysql安装完后, 表名默认区大小写. 0: 区分大小写; 1: 不区分大小写.
lower_case_table_names = 1
# 最大连接数,默认:151(允许的最大连接数16384).
max_connections = 1000
# 包的最大限制
max_allowed_packet=20M
# 免密码登入
# skip-grant-tables
4. 初始化MySQL,设置密码,允许远程登陆
[root@localhost rpm]# service mysqld start
mysql> use mysql;
mysql> select host, user, authentication_string , password_expired from user;
+-----------+---------------+-------------------------------------------+------------------+
| host | user | authentication_string | password_expired |
+-----------+---------------+-------------------------------------------+------------------+
| localhost | root | *7D0F7C5410B4AB7A25365AA269DB9DBA3FEFF74B | Y |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N |
+-----------+---------------+-------------------------------------------+------------------+
mysql> update user set authentication_string=password('123456') where user='root';
mysql> update user set host='%' where user='root' and host='localhost';
mysql> update user set password_expired='N';
mysql> flush privileges;
mysql> exit
注意:
1, 免密码登入方式(skip-grant-tables)启动,密码设置后禁用免密码登入方式
2, password_expired 为Y表示过期, N表示未过期;注意:
5. 设置开机自启动
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig --list | grep mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6. MySQL的默认安装位置
/var/lib/mysql/ #数据库目录
/usr/share/mysql #配置文件目录
/usr/bin #相关命令目录
/etc/init.d/mysqld #启动脚本
7. 常用命令
启动: service mysql start
停止: service mysql stop
查看状态: service mysql status
导入数据:LOAD DATA LOCAL INFILE '/home/package/script/table_data.csv' INTO TABLE table_name COLUMNS TERMINATED BY ',' LINES TERMINATED BY '\r\n';
链接数据库:mysql -uroot -p密码