wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
groupadd mysql
useradd -g mysql mysql
/usr/local/mysql
,没有该目录则新建[root@localhost ccc]# mv mysql-5.7.25-linux-glibc2.12-x86_64/* /usr/local/mysql/
basedir 是程序安装的目录
datadir 是mysql数据存放的目录
./bin/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql --user=mysql
touch /etc/my.cnf
在添加my.cnf中添加:
[mysqld]
server-id= 1
port=3306
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
socket=/data/mysql/mysql.sock
datadir=/data/mysql/data
basedir=/data/mysql
pid-file=/data/mysql/mysql.pid
lower_case_table_names = 1
log-error=/data/mysql/log/mysql_error.log
secure_file_priv=/data/mysql/data
user=mysql
###########slow log########################
slow_query_log = 1
long_query_time = 10
slow_query_log_file = /data/mysql/log/slow.log
###########bin log#########################
log_bin = /data/mysql/log/bin.log
binlog_format = mixed
expire_logs_days=5
###########buffer size #####################
innodb_buffer_pool_size = 10G # (50%-70% of total RAM)
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1 # may change to 2 or 0
###########other###### #####################
max_connections=500
wait_timeout=3600
interactive_timeout=3600
skip_name_resolve = 1
innodb_flush_method = O_DIRECT
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql
配置Mysql
cp support-files/my sql.server /etc/init.d/mysqld
vim /etc/profile
添加
export PATH=/usr/local/mysql/bin:$PATH
使环境变量生效
source /etc/profile
chkconfig --add mysqld #添加mysql 服务
如果执行chkconfig --list
命令,345中有off的话执行如下命令
chkconfig --level 345 mysqld on
如果学习所用的话可以reboot
重启使用如下命令查看mysql是否启动,3306是mysql的端口号
netstat -na | grep 3306
mysqld_safe $
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
mysql> Enter password: (输入刚才查询到的随机密码)
mysql> SET PASSWORD FOR 'root'@'localhost'= "Root-123";
mysql> exit (用root新密码登录)
允许root用户从任何IP能登陆到部署的服务器
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root-123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
开启防火墙mysql 3306端口的外部访问
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
mysqldump -h 192.168.189.14 -P 3306 -u root -p –database dbname table1 > 文件名.sql
或
mysqldump -u username -p dbname table1 table2 ...-> BackupName.sql
table1和table2参数表示需要备份的表的名称,为空则整个数据库备份;
mysqldump -u username -p --databases dbname2 dbname2 > Backup.sql
加上了–databases选项,然后后面跟多个数据库
mysqldump -u username -p -all-databases > BackupName.sql
mysqldump-u username -p dbname table1 table2 ... | gzip > backupfile.sql.gz
mysqldump -u username -p -d --add-drop-table dbname >d:/dbname_db.sql
-d 没有数据 --add-drop-table 在每个create语句之前增加一个drop table
mysql -u root -p < C:\BackupName.sql
或
mysql>source C:\BackupName.sql
命令格式
into outfile filename
例如:
select * from tbl_std_single into outfile "D:\\CMFT\\md\\test.sql";
把数据导入到另一个表
load data infile "/data/mysql/e.sql" into table tbl_std_single fields terminated by ' ';(指定分隔符,默认是一个tab)
如若遇到1209错误:1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
原因为mysql安全限制,全局参数中secure_file_priv为指定目录,只能导出到这个路径下(show variables like '%secure%';
查看该参数),解决办法如下
1.修改
vim /etc/my.cnf
文件,添加secure-file-priv=
(ps:null不允许导入导出,空允许导入导出到任何文件夹),这样就能导出到任何文件了,该方法需要从起mysql服务
2.根据上面查询到的路径,导入该文件目录下。如果查询为null,修改参数
set secure_file_priv='/usr/local/mysql/';
(5.7版本以上该方法失效)