写点什么

MySQL 安装配置

作者:向阳逐梦
  • 2023-05-12
    四川
  • 本文字数:2872 字

    阅读完需:约 9 分钟

MySQL 安装配置

MySQL 是最流行的关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。

MySQL 所使用的 SQL 语言是用于访问数据库的最常用标准化语言。

MySQL 由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。

MySQL 配置

1、创建 mysql 运行使用的用户 mysql:

$ /usr/sbin/groupadd mysql$ /usr/sbin/useradd -g mysql mysql
复制代码

2、创建 binlog 和库的存储路径并赋予 mysql 用户权限

$ mkdir -p /usr/local/webserver/mysql/binlog /www/data_mysql$ chown mysql.mysql /usr/local/webserver/mysql/binlog/ /www/data_mysql/
复制代码

3、创建 my.cnf 配置文件

将/etc/my.cnf 替换为下面内容

$ cat /etc/my.cnf
[client]port = 3306socket = /tmp/mysql.sock[mysqld]replicate-ignore-db = mysqlreplicate-ignore-db = testreplicate-ignore-db = information_schemauser = mysqlport = 3306socket = /tmp/mysql.sockbasedir = /usr/local/webserver/mysqldatadir = /www/data_mysqllog-error = /usr/local/webserver/mysql/mysql_error.logpid-file = /usr/local/webserver/mysql/mysql.pidopen_files_limit = 65535back_log = 600max_connections = 5000max_connect_errors = 1000table_open_cache = 1024external-locking = FALSEmax_allowed_packet = 32Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 600#thread_concurrency = 8query_cache_size = 128Mquery_cache_limit = 2Mquery_cache_min_res_unit = 2kdefault-storage-engine = MyISAMdefault-tmp-storage-engine=MYISAMthread_stack = 192Ktransaction_isolation = READ-COMMITTEDtmp_table_size = 128Mmax_heap_table_size = 128Mlog-slave-updateslog-bin = /usr/local/webserver/mysql/binlog/binlogbinlog-do-db=oa_fbbinlog-ignore-db=mysqlbinlog_cache_size = 4Mbinlog_format = MIXEDmax_binlog_cache_size = 8Mmax_binlog_size = 1Grelay-log-index = /usr/local/webserver/mysql/relaylog/relaylogrelay-log-info-file = /usr/local/webserver/mysql/relaylog/relaylogrelay-log = /usr/local/webserver/mysql/relaylog/relaylogexpire_logs_days = 10key_buffer_size = 256Mread_buffer_size = 1Mread_rnd_buffer_size = 16Mbulk_insert_buffer_size = 64Mmyisam_sort_buffer_size = 128Mmyisam_max_sort_file_size = 10Gmyisam_repair_threads = 1myisam_recoverinteractive_timeout = 120wait_timeout = 120skip-name-resolve#master-connect-retry = 10slave-skip-errors = 1032,1062,126,1114,1146,1048,1396#master-host = 192.168.1.2#master-user = username#master-password = password#master-port = 3306server-id = 1loose-innodb-trx=0 loose-innodb-locks=0 loose-innodb-lock-waits=0 loose-innodb-cmp=0 loose-innodb-cmp-per-index=0loose-innodb-cmp-per-index-reset=0loose-innodb-cmp-reset=0 loose-innodb-cmpmem=0 loose-innodb-cmpmem-reset=0 loose-innodb-buffer-page=0 loose-innodb-buffer-page-lru=0 loose-innodb-buffer-pool-stats=0 loose-innodb-metrics=0 loose-innodb-ft-default-stopword=0 loose-innodb-ft-inserted=0 loose-innodb-ft-deleted=0 loose-innodb-ft-being-deleted=0 loose-innodb-ft-config=0 loose-innodb-ft-index-cache=0 loose-innodb-ft-index-table=0 loose-innodb-sys-tables=0 loose-innodb-sys-tablestats=0 loose-innodb-sys-indexes=0 loose-innodb-sys-columns=0 loose-innodb-sys-fields=0 loose-innodb-sys-foreign=0 loose-innodb-sys-foreign-cols=0
slow_query_log_file=/usr/local/webserver/mysql/mysql_slow.loglong_query_time = 1[mysqldump]quickmax_allowed_packet = 32M
复制代码

4、初始化数据库

$/usr/local/webserver/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf  --user=mysql
复制代码

显示如下信息:

Installing MySQL system tables...2015-01-26 20:18:51 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).OK
Filling help tables...2015-01-26 20:18:57 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).OK...
复制代码

5、创建开机启动脚本

$ cd /usr/local/webserver/mysql/$ cp support-files/mysql.server /etc/rc.d/init.d/mysqld $ chkconfig --add mysqld $ chkconfig --level 35 mysqld on
复制代码

6、启动 mysql 服务器

$ service mysqld start
复制代码


7、连接 MySQL

$ /usr/local/webserver/mysql/bin/mysql -u root -p
复制代码


修改 MySQL 用户密码

mysqladmin -u用户名 -p旧密码 password 新密码
复制代码

或进入 mysql 命令行

SET PASSWORD FOR '用户名'@'主机' = PASSWORD(‘密码');
复制代码

创建新用户并授权:

grant all privileges on *.* to 用户名@'%' identified by '密码' with grant option;
复制代码

其他命令

  • 启动:service mysqld start

  • 停止:service mysqld stop

  • 重启:service mysqld restart

  • 重载配置:service mysqld reload

启动 MySQL 服务器程序,确认状态

1)启动 MySQL 服务程序

启动服务并查看状态:

[root@dbsvr1 pub]# service mysql startStarting MySQL...                                     [确定][root@dbsvr1 pub]# service mysql statusMySQL running (31724)                                 [确定]
复制代码

服务器进程为 mysqld,监听的默认端口为 TCP 3306:

[root@dbsvr1 pub]# netstat -anpt | grep mysqltcp        0      0 :::3306        :::*       LISTEN      31724/mysqld
复制代码

2)查看 MySQL 服务器进程、运行用户

提供连接服务的进程为 mysqld,由其父进程 mysqld_safe 启动。

[root@dbsvr1 pub]# ps -elf | grep mysqld
4 S root 31619 1 0 80 0 - 2834 wait 15:14 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid file=/var/lib/mysql/dbsvr1.tarena.com.pid
4 S mysql 31724 31619 0 80 0 - 252496 poll_s 15:14 pts/0 00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/dbsvr1.tarena.com.err --pid-file=/var/lib/mysql/dbsvr1.tarena.com.pid
复制代码

数据库的默认存放位置为 /var/lib/mysql:

[root@dbsvr1 pub]# ls /var/lib/mysql/auto.cnf               ibdata1      mysql               RPM_UPGRADE_HISTORYdbsvr1.tarena.com.err  ib_logfile0  mysql.sock          RPM_UPGRADE_MARKER-LASTdbsvr1.tarena.com.pid  ib_logfile1  performance_schema  test
复制代码


发布于: 刚刚阅读数: 4
用户头像

向阳逐梦

关注

人生享受编程,编程造就人生! 2022-06-01 加入

某公司芯片测试工程师,嵌入式开发工程师,InfoQ签约作者,阿里云星级博主,华为云·云享专家。座右铭:向着太阳,追逐梦想!

评论

发布
暂无评论
MySQL 安装配置_MySQL_向阳逐梦_InfoQ写作社区